简体   繁体   English

将相关实体添加到实体

[英]Adding related entities to entity

I have two tables that have many-to-many relationship .我有two tables that have many-to-many relationship I want to insert new object into first table same time with relations to second table objects using entityDataSource .我想将new object插入到第一个表中,同时使用entityDataSource与第二个表对象的关系。
Currently I'm using detailsView to enter data for new object of first table and checkboxList to choose related objects from second table.目前我正在使用 detailsView 为第一个表的新 object 输入数据,并使用 checkboxList 从第二个表中选择相关对象。

Problem is about connecting related objects to new object.问题是将相关对象连接到新的 object。 Right now I`m trying it in entityDataSource inserting event by selecting entities from second table and adding them to entityCollection of related items of entity being inserted.现在,我正在通过从第二个表中选择实体并将它们添加到要插入的实体的相关项目的 entityCollection 来尝试在 entityDataSource 插入事件中。

Problem is than I need to use objectContext of new object for getting related items but its general objectContenxt type and i cant cast it into objectContext type of my entities context.问题是我需要使用新 object 的 objectContext 来获取相关项目,但它的一般 objectContenxt 类型,我不能将它转换为我的实体上下文的 objectContext 类型。

As a general rule which we apply in case of many-to-many relationship table, please map out a third table which contains primary keys of both your related tables.作为我们在多对多关系表的情况下应用的一般规则,请 map 取出包含两个相关表的主键的第三个表。

For example, if table1 [Table1Id, ...... ] and table2 [Table2Id, .......], then create third table table3 [Table3Id, Table1Id, Table2Id]......例如,如果table1 [Table1Id,......

Not sure which form of EF [Code-First/Database First] you are using, you can simply use Table3.Table1Collection and Table3.Table2Collection properties, which needs to be attached to the context first and then save the changes.不确定您使用的是哪种形式的 EF [Code-First/Database First],您可以简单地使用 Table3.Table1Collection 和 Table3.Table2Collection 属性,这需要先附加到上下文,然后保存更改。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM