简体   繁体   中英

Insert into many to many table EF Model First

I'm using ASP.NET MVC4 Model First.

I have two tables GROUPE and TELECHARGEMENT , and the many to many table which named AUTORISERTELECHARGEMENT .

When I want to add in TELECHARGEMENT or GROUPE I do context.GROUPE.Add(groupe) , but I don't know how to add in the many to many relashionship (which contains the primary key of GROUPE and TELECHARGEMENT ).

This should solve your problem: http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-ef-4-1-building-many-to-many-relationship.aspx

This should work assuming that all the three tables belong to the same database context:

GROUPE groupe = new GROUPE { ... };
TELECHARGEMENT telechargement = new TELECHARGEMENT { ... };

AUTORISERTELECHARGEMENT at = new AUTORISERTELECHARGEMENT { groupe.ID_GROUPE, telechargement.ID_TELECHARGEMENT };

context.GROUPE.Add(groupe);
context.TELECHARGEMENT.Add(telechargement );
context.AUTORISERTELECHARGEMENT.Add(at);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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