Adding related entities to entity
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
.
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
精彩评论