C# EntityFramework.dll 中发生“System.Data.Entity.Infrastructure.DbUpdateException”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19424058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:57:09  来源:igfitidea点击:

'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll

c#wcfvisual-studioentity-framework

提问by user2656851

I wrote a rather simple code (client server based on WCF and Windows form). i was trying to update the db so that i could test my code and i encounterd the above exception. Any ideas how to solve it?

我写了一个相当简单的代码(基于WCF和Windows形式的客户端服务器)。我试图更新数据库以便我可以测试我的代码,但我遇到了上述异常。任何想法如何解决它?

      // For testing 
      public void updateTable() 
      {
        using (var db = new overlayDBEntities())
        {
            var overlaydb = new overlayData
            {
                DeviceId = "1111",
                TimestampUTC = new DateTime(1990, 1, 1, 9, 9, 9),
                OverlayData1 = "Random Text"
            };

            db.overlayData.Add(overlaydb);

            try
            {
                db.SaveChanges();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            var overlaydb1 = new overlayData
            {
                DeviceId = "1111",
                TimestampUTC = new DateTime(2000, 2, 2, 10, 10, 10),
                OverlayData1 = "seconed seconed seconed "
            };

            db.overlayData.Add(overlaydb);

            try
            {
                db.SaveChanges();
            }
            catch (Exception ec) 
            {
                Console.WriteLine(ec.Message);
            }
        }
    }

回答by Thilina H

If you need to update Row if already Exists in database so dont use context.Add();you can use as follows.

如果你需要更新数据库中已经存在的行,所以不要使用 context.Add(); 你可以使用如下。

    var overlaydb1 = new overlayData
    {
      DeviceId = "1111",
      TimestampUTC = new DateTime(2000, 2, 2, 10, 10, 10),
      OverlayData1 = "seconed seconed seconed "
    };

    try
    {
      db.overlayData.Attach(overlaydb1);
      db.ObjectStateManager.ChangeObjectState(overlaydb1, EntityState.Modified);
      db.SaveChanges();
    }

    catch (Exception ec) 
    {
      Console.WriteLine(ec.Message);
    }

回答by Freddy Yang

According to Lan,

据兰说,

"The error message details all that you need to know:

“错误消息详细说明了您需要了解的所有信息:

Additional information: The model backing the 'AppraisalDBContext' context has changed since the database was created.Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?

附加信息:自创建数据库以来,支持“AppraisalDBContext”上下文的模型已更改。考虑使用 Code First 迁移来更新数据库 ( http://go.microsoft.com/fwlink/?

It means one of your classes use in the AppraisalDBContext has changed, but the database hasn't been updated so is now out of date. You need to update this use Code First migrations."

这意味着您在 AppraisalDBContext 中使用的类之一已更改,但数据库尚未更新,因此现在已过时。您需要更新此使用 Code First 迁移。”

From The Original Post

来自原帖

回答by noushin joudzadeh

I think the problem is the type of your datetime. sqlsever has more than one datetime. if you check your inner exception, you may see the type that you are trying to put in your table and the type it actually needs. conversion cant be done automatically and you should change it in the table or convert it in your code. it can be datetime, datetime2 (7), etc.

我认为问题在于您的日期时间类型。sqlsever 有多个日期时间。如果您检查内部异常,您可能会看到您尝试放入表中的类型以及它实际需要的类型。转换不能自动完成,您应该在表中更改它或在您的代码中转换它。它可以是 datetime、datetime2 (7) 等。

回答by user3218010

Below is the procedure to solve the System.Data.Entity.Infrastructure.DbUpdateException. Please follow the instructions.

下面是解决 System.Data.Entity.Infrastructure.DbUpdateException 的过程。请按照说明操作。

Happy Coding !!!

快乐编码!!!

https://msdn.microsoft.com/en-us/data/dn579398.aspx

https://msdn.microsoft.com/en-us/data/dn579398.aspx

Best Regards, Thet Tin Oo

最好的问候, Thet Tin Oo

回答by Usman lqbal

This same Error is also appear when user forget to add Primery keyin particular table .. and then add data through application.

当用户忘记添加Primery key特定表 .. 然后通过应用程序添加数据时,也会出现同样的错误。

回答by AJIT AGARWAL

using Code first approach .
How to define primary key in Web API.

Eg:- Model Class
  public class Categories
    {
        [Key]
        public int cateId { get; set; }
        public string cateName { get; set; }
    }
Controller Class:-  Categories Controller
 public bool AddCate(Categories preg)
        {            
            db.Categories.Add(preg);
            db.SaveChanges();
            return true;
        }
Context Db Class
  public System.Data.Entity.DbSet<TestingWebApi.Models.Categories> Categories { get; set; }
 Error:-"System.Data.Entity.Infrastructure.DbUpdateException",

回答by Triet Pham

Multiple records of data with the same primary key overlap when add, causing the same error

多条主键相同的数据记录重叠时add,导致同样的错误

You should review your primary key settings

您应该检查您的主键设置