<><>UPDATE: 我最近只字不提几个字句,因此,我看上去,让人们知道我下面提供的建议是最好的。 自2006年以来 最初,我开始忙于在旧的无关键数据库上制定实体框架,我已认识到,卢旺达武装部队所能做的最出色工作是扭转代码第一。 在如何做到这一点方面,有一些很好的文章。 就在后面,然后,当你想增加关键内容时,将数据说明用于“伪造”钥匙。
例如,我要说的是,我知道我的表格<编码>Orders,虽然它没有主要钥匙,但只保证每个客户有一个订单号码。 由于这些是表上头两栏,我设立了第一栏,以研究:
[Key, Column(Order = 0)]
public Int32? OrderNumber { get; set; }
[Key, Column(Order = 1)]
public String Customer { get; set; }
By doing this, you re basically faked EF into believing that there s a clustered key composed of OrderNumber and Customer. This will allow you to do inserts, updates, etc on your keyless table.
如果你不熟悉《实体框架法》第一版,就开始并找到一部关于实体框架法典的良好指导。 然后,就《反向法第一》(该法首先使用现有的数据库)。 之后,我刚刚回过来,再次审视我的主要建议。
www.un.org/Depts/DGACM/index_spanish.htm
首先,正如其他人所说的那样,最好的选择是增加表格的主要关键内容。 完全停止。 如果你能够这样做,就不再做。
但是,如果你能够 t,或只是仇恨,那么,没有主要钥匙,就能够这样做。
在我的案件中,我正与遗产制度合作(最初是一份安达400号船的固定档案,然后运到T-SQL)。 因此,我不得不找到办法。 这是我的解决办法。 以下是我使用实体框架6.0(最新版本的这份著作)。
Right-click on your .edmx file in the Solution Explorer. Choose "Open With..." and then select "XML (Text) Editor". We re going to be hand-editing the auto-generated code here.
Look for a line like this:
<EntitySet Name="table_name" EntityType="MyModel.Store.table_name" store:Type="Tables" store:Schema="dbo" store:Name="table_nane">
Remove store:Name=“table_name”
from the end.
Change store:Schema="whatever"
to Schema="whatever"
Look below that line and find the <DefiningQuery>
tag. It will have a big ol select statement in it. Remove the tag and it s contents.
Now your line should look something like this:
<EntitySet Name="table_name" EntityType="MyModel.Store.table_name" store:Type="Tables" Schema="dbo" />
We have something else to change. Go through your file and find this:
<EntityType Name="table_name">
几乎你可能看到一些评论的文本警告你,它没有确定主要的关键,因此,关键是推算的,定义只是读表/概览。 您可以离开或删除。 我删除了这一句。
Below is the <Key>
tag. This is what Entity Framework is going to use to do insert/update/deletes. SO MAKE SURE YOU DO THIS RIGHT. The property (or properties) in that tag need to indicate a uniquely identifiable row. For instance, let s say I know my table orders
, while it doesn t have a primary key, is assured to only ever have one order number per customer.
So mine looks like:
<EntityType Name="table_name">
<Key>
<PropertyRef Name="order_numbers" />
<PropertyRef Name="customer_name" />
</Key>
很严重,没有这样做。 让我们说,即使永远不会发生重复,但只有两行才能进入我的系统,其编号和客户名称相同。 Who! 那是我不使用钥匙的。 因此,我利用实体框架删除。 由于我知道重复是今天的唯一命令,我这样做:
var duplicateOrder = myModel.orders.First(x => x.order_date == DateTime.Today);
myModel.orders.Remove(duplicateOrder);
Guess what? I just deleted both the duplicate AND the original! That s because I told Entity Framework that order_number/cutomer_name was my primary key. So when I told it to remove duplicateOrder, what it did in the background was something like:
DELETE FROM orders
WHERE order_number = (duplicateOrder s order number)
AND customer_name = (duplicateOrder s customer name)
And with that warning... you should now be good to go!