开发者

Adding related entities to entity

开发者 https://www.devze.com 2023-03-08 21:48 出处:网络
I have two tables that have many-to-many relationship. I want to insert new object into fi开发者_如何学Crst table same time with relations to second table objects using entityDataSource.

I have two tables that have many-to-many relationship. I want to insert new object into fi开发者_如何学Crst table same time with relations to second table objects using entityDataSource.

Currently I'm using detailsView to enter data for new object of first table and checkboxList to choose related objects from second table.

Problem is about connecting related objects to new 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.

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.


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.

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

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.

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消