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

痴心易碎 提交于 2020-01-16 22:23:24

问题


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:


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.




回答2:


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();


来源:https://stackoverflow.com/questions/26263733/adding-new-child-records-to-a-parent-table-in-entity-framework-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!