[英]In Entity framework load child in parent entity (non primary key association)
I am very new to Entity Framework and so I don't know the technical terms use in it. 我是Entity Framework的新手,所以我不知道其中使用的技术术语。 I am sorry for my bad English.
对不起,我的英语不好。
I am working in a project which has entity framework in it and has an .edmx
file. 我正在一个有实体框架并且有一个
.edmx
文件的项目中工作。 The file has entities created in it. 该文件具有在其中创建的实体。
So I created 2 entities and named it TableA and TableB. 因此,我创建了2个实体,并将其命名为TableA和TableB。 pid is entity key of TableA and cid is entity key of TableB .
pid是TableA的实体密钥,cid是TableB的实体密钥。 And created many to one relationship in it.
并在其中建立了多对一关系。 ie TableA row1 can have TableB_cid=1 TableA row2 can also have TableB_cid=2
即TableA row1可以具有TableB_cid = 1 TableA row2也可以具有TableB_cid = 2
And then I did "Generate database from model" 然后我做了“从模型生成数据库”
CREATE TABLE [TableA] (
[pid] nvarchar(max) NOT NULL,
[name] nvarchar(max) NOT NULL,
[TableB_cid] nvarchar(max) NOT NULL
);
GO
CREATE TABLE [TableB] (
[cid] nvarchar(max) NOT NULL,
[name] nvarchar(max) NOT NULL
);
pid|name|TableB_cid
--------------------
1 | a | 2
2 | b | 2
3 | c | 1
cid|name
------------
1 | s
2 | f
Now in the C# code i wrote this, 现在在C#代码中,我写了这个
TableA obj = repository.All().Single(w=>w.pid == "1")
context.Entry<TableA>(obj).Reference<TableB>(o => o.TableB).Load();
Now this will load the TableB whose cid=1 (but what i actually wanted was to load TableB whose cid=2). 现在,这将加载cid = 1的TableB(但是我真正想要的是加载cid = 2的TableB)。
I think its matching primary key to primary key and not TableB_cid (TableA) to cid (TableB). 我认为其匹配的主键为主键,而不是TableB_cid(TableA)到cid(TableB)。
So what i am doing wrong please help. 所以我做错了请帮忙。
Edit: 编辑:
In short I am looking for a way where I can add association of a Non Primary column of table A with a Primary column of table B in Entity Framework 4.0. 简而言之,我正在寻找一种方法,可以在Entity Framework 4.0中添加表A的非主列与表B的主列的关联。 And I don't want to use linq queries and joins.
而且我不想使用linq查询和联接。 I have tried but I cant a find a way or option in edmx file where i can do it.
我已经试过,但我不能找到一个在EDMX文件的方式或选项,我可以做到这一点。
Thanks,
谢谢,
M 中号
var childs = from c in context.Children
where c.cid = o.Child_cid;
I think that should help you. 我认为这应该对您有帮助。
I'm not sure I understand the question, but in your example, are you simply attempting to access the TableB entity of the TableA with Id 1? 我不确定我是否理解问题,但是在您的示例中,您是否只是尝试使用ID 1访问TableA的TableB实体?
If so, how about this: 如果是这样,如何处理:
TableA obj = repository.All().Single(w=>w.pid == "1");
TableB SecondTable = obj.TableB;
SecondTable.cid should be 2, is and obj.pid should be 1. If not, what values are you getting? SecondTable.cid应该为2,是,而obj.pid应该为1。如果不是,那么您得到什么值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.