[英]Trying to set up a 0..1 : 1 relationship with SQL and Entity Framework
Let's say there are two tables in our db. 假设数据库中有两个表。
Table1 has fields: 表1具有以下字段:
Id
Field 2
Field 3
Table2 has fields: 表2具有以下字段:
Table1Id
Field 2
Field 3
Right now Table1's PK is Id, and Table 2's PK is Table1ID (which makes sense.) 现在,表1的PK是Id,表2的PK是Table1ID(这很有意义。)
Table1 may or may not have a Table2 entity related to it. Table1可能有也可能没有与之相关的Table2实体。 (depending on whether Table1.Field2's value is 3, but anyways..) (取决于Table1.Field2的值是否为3,但无论如何..)
I've set up a FK on Table2 such that Table1.Id is related to Table2.Table1Id 我在Table2上设置了FK,以使Table1.Id与Table2.Table1Id相关
My end goal here is to, when accessing Table1 via entity framework (db-first, by the way), I can get to my Table2 entity by just Table1.Table2 (should be null or populated). 我这里的最终目标是,当通过实体框架(顺便说一句db-first)访问Table1时,我可以通过Table1.Table2(应该为null或填充)进入Table2实体。
Right now, I have to do something like Table1.Table2s.First() 现在,我必须要做类似Table1.Table2s.First()的操作
What relationship am I missing to have this First() unnecesarry? 我缺少这个First()不需要什么关系?
Note: Table 1 already exists in our DB and is used. 注意:表1已存在于我们的数据库中并已使用。 Table 2 is part of a new thing. 表2是新事物的一部分。
Here's my current creation of Table2 leading to my problem 这是我当前创建的Table2导致的问题
CREATE TABLE Table2(
Table1Id NUMERIC(18, 0),
Field2 BIT NOT NULL,
Field3 BIT NOT NULL,
CONSTRAINT Table2_pk PRIMARY KEY (Table1Id ),
CONSTRAINT FK_Table1ID
FOREIGN KEY (Table1Id)
REFERENCES Table1(Id)
);
Even stranger, everything looks fine in the EDMX 更陌生的是,EDMX中的一切看起来都很好
Further, the EDXM file's association checks out with multiplicity 1 and 0..1 此外,EDXM文件的关联以多重性1和0..1签出。
EDIT: 编辑:
Throughout enough fuddling this works. 在整个过程中,一切都弄乱了。 I reverted my model and made the connection from scratch /again/ and it magically works. 我恢复了模型并再次/从头开始建立连接,并且神奇地起作用了。 I'll try to think about why this work and edit if I figure out why. 我将尝试考虑为什么要这样做,如果发现原因,请进行编辑。
Are you using DB Fist or Code First? 您在使用DB Fist还是Code First? With DB First, it should just work: 使用DB First,它应该可以正常工作:
Here is an image of what I get when I use EF DB first: 这是我第一次使用EF DB时得到的图像:
And here's an image showing that this setup does correctly give a singleton relationship: 这是一张图像,显示此设置确实正确给出了单例关系:
Also, if you edit your .edmx
file with a text editor, the Association
properties should be defined something like this: 另外,如果您使用文本编辑器编辑.edmx
文件,则应该定义Association
属性,如下所示:
<Association Name="FK_Table1ID">
<End Role="Table1" Type="XOneModel.Store.Table1" Multiplicity="1" />
<End Role="Table2" Type="XOneModel.Store.Table2" Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="Table1">
<PropertyRef Name="id" />
</Principal>
<Dependent Role="Table2">
<PropertyRef Name="Table1Id" />
</Dependent>
</ReferentialConstraint>
</Association>
Can you double-check that? 你可以仔细检查一下吗?
If you are using Code-First, then you need to make sure you are defining your relationships correctly and that you define the Table2
member in Table1
as an single instance property and not a collection...if you're using CF, can you post your model code? 如果您使用的是Code-First,则需要确保正确定义了关系,并且将Table1
的Table2
成员定义为单个实例属性而不是集合...如果使用CF,可以吗?发布您的模型代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.