在实体框架中向父表添加新的子记录6 [英] Adding new child records to a parent table in entity framework 6

查看:268
本文介绍了在实体框架中向父表添加新的子记录6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表:

帐户: Id,Name

用户: Id,AccountId,Name

User: Id, AccountId, Name

UserDetail :Id,UserId,Phone

UserDetail: Id, UserId, Phone

Entitites:

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;}
}

如您所见,Account to User是一对多的关系,所以我的Account实体有Users DbSet。而UserDetail的用户是一对一的关系。

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

我收到以下错误:
依赖参考参考中的属性被映射到商店生成的列。列:'Id'。

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

想法?

推荐答案

参考代码中的依赖属性映射到存储生成的列。列:Id

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

基本上,它表示UserDetail.Id(它是依赖属性)具有参照约束,也是一个存储生成列,即autoincrement。

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.

解决方案是删除自动增量属性或在UserDetail或UserDetail用户上添加不同的FK。

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

经验教训:
1.绝对具有名称为类/表的标识列。例如,如果可以,将您的身份(PK)列命名为表的名称作为前缀。所以在我的问题,我应该重命名所有的ID列AccountId,UserId,UserDetailId。

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.

为什么?

EF的本意是给我一个明显的线索来查看UserDetailId的映射。没有这个,我一直在想,哪个映射是有问题的。

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.

这篇关于在实体框架中向父表添加新的子记录6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆