简体   繁体   English

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

[英]'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll

I wrote a rather simple code (client server based on WCF and Windows form).我写了一个相当简单的代码(基于WCF和Windows形式的客户端服务器)。 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?任何想法如何解决它?

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

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

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

I think the problem is the type of your datetime.我认为问题在于您的日期时间类型。 sqlsever has more than one datetime. sqlsever 有多个日期时间。 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.它可以是 datetime、datetime2 (7) 等。

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.这意味着您在 AppraisalDBContext 中使用的类之一已更改,但数据库尚未更新,因此现在已过时。 You need to update this use Code First migrations."您需要更新此使用 Code First 迁移。”

From The Original Post来自原帖

Multiple records of data with the same primary key overlap when add , causing the same error多条主键相同的数据在add时重叠,导致同样的错误

You should review your primary key settings您应该检查您的主键设置

In my case, i have errors while generate model (.edmx file) and entities (.tt file).就我而言,我在生成模型(.edmx 文件)和实体(.tt 文件)时出错。 Regenerate it solve the problem重新生成它解决问题

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",

Below is the procedure to solve the System.Data.Entity.Infrastructure.DbUpdateException.下面是解决 System.Data.Entity.Infrastructure.DbUpdateException 的过程。 Please follow the instructions.请按照说明操作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityFramework.dll中发生类型为&#39;System.Data.Entity.Infrastructure.DbUpdateException&#39;的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll 发生System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException occurred 发生类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException System.Data.Entity.Infrastructure.DbUpdateException问题 - System.Data.Entity.Infrastructure.DbUpdateException Issue MVC EF-System.Data.Entity.Infrastructure.DbUpdateException - MVC EF - System.Data.Entity.Infrastructure.DbUpdateException 如何解决 System.Data.Entity.Infrastructure.DbUpdateException - How to solve System.Data.Entity.Infrastructure.DbUpdateException 递归树方法上的System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException on recursive Tree method EntityFramework.dll中发生了&#39;System.Data.Entity.ModelConfiguration.ModelValidationException&#39; - 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll 将数据添加到数据库(错误:System.Data.Entity.Infrastructure.DbUpdateException) - Adding Data to Database (Error: System.Data.Entity.Infrastructure.DbUpdateException)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM