asp.net mvc - Adding a many to Many relationship in C#.NET Entity -
this question has answer here:
i use assistance idea i'm working on.
i need many many relationship between identitymodel , bookmodel. this.
now in code have in identitymodel.
public class applicationuser : identityuser { public virtual icollection<book> books { get; set; } ... } public class applicationdbcontext : identitydbcontext<applicationuser> { ... public system.data.entity.dbset<talkbooks.models.book> books { get; set; } }
and in book model
public class book { public int id { get; set; } public string name { get; set; } public string isbn { get; set; } public virtual icollection<applicationuser> users { get; set; } }
now want users able select books own database (to keep simple there 1 book in there right , want use dropdown list selecting it) have stored in database.
i thought editing scaffolded managecontroller include method takes care of adding books account.
now when create database (code-first entity) creates database follows:
i left out discussionmodel , messagemodel moment. need code needed make work inside controller. have tried much, , keep failing, know due fact i'm beginner , lacking experience. want learn can teach me. appreciated!
your applicationdbcontext uses entityframework has nothing identity 2.0 in first place entityframework itself.
to create relationships in entity framework have 2 options:
- using annotations
- using fluent api
see this post examples on both options. prefer fluent api approach think personal thing. in case example this:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<book>() .hasmany<applicationuser>(b => b.users) // book entity has many users .withmany(u => u.books); // user entity includes many book entities }
Comments
Post a Comment