Alright so I'm a bit stuck at the moment. We've creating an MVC application using EF, fluent api, etc. and I'm having some trouble getting new records into a join table at the moment.
DB Table structure
User
(pk)ID
Username
passwordhash
etc.
etc.
DDRRole
(pk)ID
(fk)RoleId
(fk)DDRId
UserRole
(pk,fk)UserId
(pk,fk)DDRRoleId
Role
(pk)ID
Name
DDR
(pk)ID
Name
class structure
the classes for the tables are what you'd expect having the props for the columns and for example User has anICollection<DDRRole> DDRRole
and DDRRole has an ICollection<User> User
Here's the fluent api part (not all of it, but the part that deals with the join table):
modelBuilder.Entity<User>()
.HasMany(uc => uc.DDRRole)
.WithMany(dd => dd.User)
.Map(x =>
{
x.MapLeftKey("UserID");
x.MapRightKey("DDRRoleID");
x.ToTable("UserRole");
});
so what's going wrong is when we're creating a new user that part is fine, but as we're creating the new user we want to give that user a DDRRole (which is already in database), which will then get added to the UserRole table. I've searched a ton on this with what to do supposedly with fluent api and other things but haven't gotten this to work yet.
thanks in advance for the help