Adding new child records to a parent table in entity framework 6

entity-framework-6 referential-integrity

Question

I have 3 tables:

Account: Id, Name

User: Id, AccountId, Name

UserDetail: Id, UserId, Phone

Entitites:

public partial class Account
{
    public Account()
    {
        this.Users = new HashSet<User>();
    }

    public int Id{get;set;}
    public string Name{get;set;}
    public virtual ICollection<User> Users{get;set;}
}

public partial class UserDetail
{
    public int Id{get;set;}
    public string Phone {get;set;}
    public virual User User {get;set;}
}

public partial class User
{
    public User()
    {
        this.Accounts = new HashSet<Account>();
    }

    public int Id{get;set;}
    public virtual ICollection<Account> Accounts {get;set;}
    public virtual UserDetail UserDetail{get;set;}
}

As you can see, Account to User is an one to many relationship so my Account entity has Users DbSet. Whereas User to UserDetail is a one-to-one relationship.

I am currently trying to insert a user and userdetail record to an existing account. So my code looks like this:

var newUserDetail = new UserDetail();
newUserDetail.Phone = 014109842;

var newUser = new User();
newUser.Name = "John Smith";
newUser.UserDetail = newUserDetail;

var currentAccount = _dbContext.Accounts.First(a => a.Id == 100);
currentAccount.User.Add(newUser);
_dbContext.SaveChanges();

I'm getting the following error: "A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'."

Thoughts?

1
0
10/9/2014 7:36:52 AM

Accepted Answer

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: "Id"

I researched this error in more detail and came across this post: A dependent property in a ReferentialConstraint is mapped to a store-generated column

Basically, it says that the UserDetail.Id (which is dependent property) has a referential constraint and is also a store generated column i.e. autoincrement.

The solution was to either remove the autoincrement property or add a different FK on either User to UserDetail or on UserDetail to User.

Lessons learnt: 1. Definitely have identity columns with name of the class/table. For example, if you can, name your identity (PK) column with the name of table as a prepend. So in my question, I should really rename all the Id columns to AccountId, UserId, UserDetailId.

Why?

The innerexception that EF had would have given me an obvious clue to look at the mapping on UserDetailId. Without this, I kept wondering which mapping was the problematic one.

1
5/23/2017 12:32:52 PM

Popular Answer

Try this.

var newUserDetail = new UserDetail();
newUserDetail.Phone = 014109842;
_dbContext.UserDetail.Add(newUserDetail);

var newUser = new User();
newUser.Name = "John Smith";
_dbContext.User.Add(newUser);

_dbContext.saveChanges(); // save changes so the db can generate ID's

newUser.UserDetail = newUserDetail;

var currentAccount = _dbContext.Accounts.First(a => a.Id == 100);
currentAccount.User.Add(newUser);
_dbContext.SaveChanges();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow