实体框架插入错误? [英] Entity Framework Insert bug?

查看:85
本文介绍了实体框架插入错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用EF 6.2.0 DB,SQL Server 2017

I used EF 6.2.0 DB first, SQL Server 2017

我创建了一个表格对此进行测试

I created a table to test this

create table Person
(
    ID int primary key,
    Name varchar(50)
)

我创建了一个要插入的表单,这是按钮单击事件:

And I created a form to insert, this is button click event:

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
    Person p = new Person();
    p.id = Convert.ToInt32(txbID.Text);
    p.name = txbName.Text;

    try
    {
        db.People.Add(p);
        db.SaveChanges();
    }
    catch (Exception ex)
    {
        Console.WriteLine("###Ex:" + ex.ToString());
        MessageBox.Show(ex.ToString());
    }
}

首先,我插入一个具有ID = 1的人.

First, I insert a person with ID = 1.

然后,我用ID = 1插入另一个人,这导致了此异常:

Then, I insert another person with ID = 1 and it caused this exception:

System.Data.SqlClient.SqlException:违反了PRIMARY KEY约束'PK__Person__3213E83F397C4503'.无法在对象"dbo.Person"中插入重复的密钥.重复的键值为(1).

System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK__Person__3213E83F397C4503'. Cannot insert duplicate key in object 'dbo.Person'. The duplicate key value is (1).

最后,我插入一个具有ID = 2的人,它仍然显示相同的异常:

Finally, I insert a person with ID = 2 and it still show the same exception:

System.Data.SqlClient.SqlException:违反了PRIMARY KEY约束'PK__Person__3213E83F397C4503'.无法在对象"dbo.Person"中插入重复的密钥.重复的键值为(1).

System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK__Person__3213E83F397C4503'. Cannot insert duplicate key in object 'dbo.Person'. The duplicate key value is (1).

在第一个异常之后,插入任何ID将导致相同的异常The duplicate key value is (1).我认为这是一个错误.

After the first exception, insert any ID will cause the same exception The duplicate key value is (1). I think it's a bug.

推荐答案

不,这不是bug-我怀疑您还不太了解DbContext的工作原理.

No, it's not a bug - I suspect you haven't really understood how the DbContext works.

当您尝试使用显然会产生错误的ID = 1插入第二个人时,该对象(导致此错误)现在是DbContext(db.People集合的一部分)的一部分.

When you try to insert the second person with ID = 1 which will obviously create the error, that object (that causes this error) is now part of the DbContext (of the db.People collection).

如果您使用ID = 2添加其他人,则您使用ID=1的有问题的"第二人是DbContext(db.Person)的静止部分-除非您已经专门清理过(删除了那个麻烦的人,或者完全创建了一个新的DbContext.)

If you add another person, with ID = 2, your "problematic" second person with ID=1 is still part of the DbContext (db.Person) - unless you've specifically cleaned up (removed that troublesome person, or created a new DbContext altogether).

因此,在添加具有ID = 2的人之后,您的DbContext现在有一个ID = 1的人和另一个ID = 2的人,当调用.SaveChanges()时将被保存-这将课程再次失败,并显示相同的错误-这是与以前相同的问题……

So after adding the person with ID = 2, your DbContext now has a Person with ID=1 and another one with ID=2 to be saved when calling .SaveChanges() - and that will OF COURSE again fail with the same error - it's the same problem as before......

解决此问题的一种方法是明确地 btnSubmit_Click方法中创建DbContext:

One way to solve this would be to explicitly create the DbContext inside your btnSubmit_Click method:

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
    using (var db = new YourDbContextType())
    {
        Person p = new Person();
        p.id = Convert.ToInt32(txbID.Text);
        p.name = txbName.Text;

        try
        {
             db.People.Add(p);
             db.SaveChanges();
        }
        catch (Exception ex)
        {
             Console.WriteLine("###Ex:" + ex.ToString());
             MessageBox.Show(ex.ToString());
        }
    }
}

这篇关于实体框架插入错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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