-1

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

makinitmine
  • 94
  • 2
  • 10

1 Answers1

0

Ok so I figured out what was going on (kind of)....so inside my create method I was passing my User object that just got created to the method that was trying to add the role and something with that wasn't working well with each other.....long story short I passed in the new user's id and then did a db.find(userid) into a new object and there added the role to that....upon savechanges() the insert worked.

makinitmine
  • 94
  • 2
  • 10
  • What about this problem? Could you have a look at please? Thanks in advance... http://stackoverflow.com/questions/29333787/how-to-create-lookup-table-and-define-relationships – Jack Mar 29 '15 at 19:39