主键实体框架代码第一次违反 [英] Violation of primary key Entity Framework Code First

查看:96
本文介绍了主键实体框架代码第一次违反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始用C#,我想使自己的数据库。

I have started with C# and I wanted to make my own DB.

我有两个型号

public class AModel 
{
    public Guid ID { get; private set; }
    public string Name { get; set; }
    public int Count { get; set; }
    public AModel()
    {
        this.ID = Guid.NewGuid();
    }
}

public class BModel 
{
    public Guid ID { get; private set; }
    public string Name { get; set; }
    public AModel Model { get; set; }
    public BModel()
    {
        this.ID = Guid.NewGuid();
    }
}

当我尝试BModel保存到数据库,我得到这个错误:

When I try to save BModel to DB, I get this error:

PRIMARY KEY约束'PK_dbo.AModels的相关规定。不能插入对象dbo.AModels'
重复键。重复的键值为
(48ee1711-8da4-46c1-a714-19e985211fed).\r\\\
The声明已经
终止。

Violation of PRIMARY KEY constraint 'PK_dbo.AModels'. Cannot insert duplicate key in object 'dbo.AModels'. The duplicate key value is (48ee1711-8da4-46c1-a714-19e985211fed).\r\nThe statement has been terminated.

我认为这将这个

modelBuilder.Entity<BModel>().HasRequired(t => t.Model).WithMany();



但它看起来像我完全丧失。任何人可以帮我这个简单的例子?

but it looks like I am completely lost. Could anybody help me with this simple example?

推荐答案

您的评论透露的重要信息。当您从组合框添加AMODEL你BModel,这将成为届时从你的DbContext分离。当你把它添加到您的模型,实体框架会认为你有一个新的对象。

Your comment reveals vital information. When you add that AModel from your combobox to your BModel, it will have become detached from your DbContext by then. When you then add it to your model, Entity Framework will think that you have a new object.

既然你已经配置为DatabaseGenerationOptions.None你的ID,它将使用主键你提供你自己。在你的情况,这是分离对象的PK。因此,当EF试图插入该条目就会抛出上述异常,因为与该键的实体是已经在那里

Since you have your Ids configured as DatabaseGenerationOptions.None, it will use the primary key you provide yourself. In your case this is the PK of the detached object. Thus, when EF tries to insert this entry it will throw the above exception because an entity with that key is already in there.

有解决这几个方面:


  • 检索现有实体

这实体将在检索连接到你的环境,让您用这个。这意味着额外的查找不过:首先让他们进入组合框,然后使用该ID与实体组合框从数据库中重新找回

This entity will be attached to your context upon retrieval, allowing you to use this. This means an extra lookup however: first to get them into the combobox and then to use the Id from the entity in the combobox to retrieve it again from the database.

例用法:

AModel Get(AModel detachedModel)
{
    using(var context = new MyContext())
    {
        return context.AModels.Single(x => x.ID == detachedModel.ID);
    }
}




  • 附加现有的模式

  • 这应该只是做实体框架知道该实体已存在于数据库中。

    This should just make Entity-Framework aware that the entity already exists in the database.

    using(var context = new MyContext())
    {
        context.AModels.Attach(detachedModel);
    }
    



    其他的方式来连接是由国家设定为不变。

    Other ways to attach is by setting the state to Unchanged

    context.Entry(detachedModel).State = EntityState.Unchanged;
    

    或修改(如果你也变更值)

    or Modified (in case you also changed values)

    context.Entry(detachedModel).State = EntityState.Modified;
    

    这篇关于主键实体框架代码第一次违反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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