简体   繁体   English

在实体框架中,在父实体中加载子级(非主键关联)

[英]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.

相关问题 实体框架代码优先-通过主键将子实体添加到父实体 - Entity Framework Code First - Add Child Entity to Parent by Primary Key 实体框架:父和子主键具有相同名称的继承 - Entity Framework: Inheritance with same name for parent and child primary key 实体框架使用父级加载子级 - Entity Framework Load Child With Parent 使用 Entity Framework 4.1 Fluent API 在非主键字段上创建关联 - Create association on non-primary key fields with Entity Framework 4.1 Fluent API 实体框架:在关联中使用非主唯一键的替代解决方案 - Entity Framework: Alternate solution to using non primary unique keys in an association 实体框架-当孩子有复合主键时为孩子父母-插入错误 - Entity Framework - Parent to Children When Child Has Composite Primary Key - Insert Error 如何在Entity Framework Code First中使用字符串主键延迟加载子对象? - How to Lazy Load child object with string primary key in Entity Framework Code First? 实体框架 - 将子类外键与父类主键相关联 - Entity Framework - relating subclass foreign key to parent class primary key Entity Framework 5.0非主键的复合外键-可能吗? - Entity Framework 5.0 composite foreign key to non primary key - is it possible? 实体框架代码优先 - 非主键字段的外键 - Entity Framework Code First - Foreign Key to non primary key field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM