An exception of type ‘Microsoft.EntityFrameworkCore.DbUpdateException’ occurred in Microsoft.EntityFrameworkCore.dll

I am having trouble at very long time. Lets imagine this example:

public class Coordinate {

     public int id {get;set;}
     public int x {get;set;}
     public int y {get;set;}
}
public class Planet {

     public int id {get;set;}
     public string name {get;set;}
     public Coordinate coordinate {get;set;}
}

I have created two models, and the model Planet has the model Coordinate as attribute.
Now imagine somewhere in the code I create one coordinate and it is stored in database.
Imagine this is the coordinate:

Coordinate c = new Coordinate();
c.x = 1;
c.y = 2;

Then I add it to my database and it is saved.

But when I create a planet and I do:

planet.coordinate = c;

And then I try to add it to database I have the following error:

An exception of type ‘Microsoft.EntityFrameworkCore.DbUpdateException’
occurred in Microsoft.EntityFrameworkCore.dll but was not handled in
user code

Additional information: An error occurred while updating the entries.
See the inner exception for details.

I know I can change the attribute public Coordinate coordinate to public int coordinate_id but I want to do this with the Coordinate model instead.

I am using ASP NET CORE 1.0

Cumps

1

Your problem is that at this point, c already has an Id.

With planet.Add, the planet and all coordinates attached to it will be set to Added in your DbSet’s and upon calling SaveChanges, insert statements will be created. (Here I assume an autoincrement on your column and your Id property)

When SaveChanges is completed, EF will see that the planet is in the database, but the Id of the just added coordinate is different (it was altered by DBMS, so now the coordinate is twice in your database, with two different Id’s), so it will expect something went wrong and throw this exception.

When you don’t have problems with duplicate entries, set the Id to null or 0. Otherwise, there are two solutions:

-Set only the FK property, not the navigation property

or

-Call SaveChanges only once (for example, just add the planet, however with added coordinates relationship fixup should lead to the same result)

4

Having a look at the inner exception will give you a more detailed look at what is going wrong.

To do this, in debug mode, when the exception shows. Click view detail and follow the tree until you find inner exception.

There may be duplicate rows, primary key issues or structure issues.

3

I was having the same problem and I realized that I was creating more than only one instance to access the database.
So the solution I took was creating a class that made only one access.

class SingletonContext {
    private static Context _context;
    public static Context GetInstance() {
        if (_context == null) {
            _context = new Context();
        }
        return _context;
    }
}

And in every access to the database layer I call the GetInstance(), like this:

private static Context _context = SingletonContext.GetInstance();

I also got the similar error, while creating the post method but I resolved this by doing the following

Make sure that you have included the following code in

  1. Constructor
    In this import the interface file of which the foreign is key belongs to

  2. HttpPost method

     [HttpPost]
     [ProducesResponseType(204)]
     [ProducesResponseType(400)]
     [ProducesResponseType(404)]
    
     //IMPORTANT TO INCLUDE [FromQuery]
     public IActionResult CreateBatch([FromQuery] int trackId, 
      [FromBody] BatchDto batchCreate)
     {
         if(batchCreate == null)
             return BadRequest(ModelState);
    
         var batch = _batchInterface.GetBAtches()
             .Where(c => c.batchName.Trim().ToLower() == 
              batchCreate.batchName
             .TrimEnd().ToLower());
    
         if (batch == null)
             return BadRequest(ModelState);
    
         var batchMap = _mapper.Map<Batch>(batchCreate);
    
         batchMap.track  = _trackInterface.GetTrack(trackId);  //Important
    
         if (!ModelState.IsValid)
             return BadRequest(ModelState);
    
         if(!_batchInterface.CreateBatch(batchMap))
         {
             ModelState.AddModelError("", "Something went in the 
             creation of batch");
             return StatusCode(500, ModelState);
         }
    
         return Ok("Successfully created Batch");
    
     }
    

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật