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?
Thanks,
Ren