Trouble with insert with m:n

Posts   
 
    
Ren
User
Posts: 42
Joined: 01-Jul-2005
# Posted on: 09-Nov-2007 02:05:53   

I am trying to insert a new user into a database. The user has a relationship with a role. There is a many to many table (UserRole) to handle the fact that a user can have many roles and vis versa.

I create my user like so:



UserEntity user = new UserEntity();
user.Fname = "Joe";
user.Lname = "Blow";

UserRoleEntity userRoleEntity1 = new UserRoleEntity();
userRoleEntity.RoleId = 3;  // I know the department, but I can't set the UserId since it's an insert.

UserRoleEntity userRoleEntity1 = new UserRoleEntity();
userRoleEntity.RoleId = 4;

user.UserRoles.Add(userRoleEntity1);
user.UserRoles.Add(userRoleEntity2);



Now when I try to call adapter.SaveEntity I am getting an error. The User table has an Identity key and the m:n table doesn't have identity keys. The UserId and RoleId combined make the key of that table.

So when I call the following:



            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
               updateComplete = adapter.SaveEntity(user, false, true);
            }



I get an error about: ORMQueryExecutionException: INSERT statement conflicted with COLUMN FOREIGN KEY

What am I doing wrong? This should work right? confused

Thanks, Ren

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Nov-2007 10:27:03   

You have posted this question twice. I'll close this thread so we can follow up in one place. (Here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11824)