简体   繁体   English

在实体框架中更新子实体不起作用

[英]Updating Child Entities in Entity Framework not working

Hello I have a class called Property: 您好,我有一个叫做Property的类:

It has the following child entities:- 它具有以下子实体:

Public class Property
{

  public virtual ICollection<PropertyUrl> Property_URLs { get; set; }
  public virtual ICollection<BrochureData> Property_Brochures { get; set; }
  public virtual ICollection<ImageSortOrder> Property_ImageSortOrders { get; set; }

}

When I try updating Property and along with it the Child Entities, I see new records created for Child Entities instead of existing one's updated. 当我尝试更新Property及其子实体时,会看到为子实体创建的新记录,而不是现有实体的已更新记录。 For example I send ImageSortOrder in Property to be updated:- 例如,我在属性中发送ImageSortOrder进行更新:-

           //Property Image
            ImageSortOrder imageSortOrder = new ImageSortOrder();
            imageSortOrder.FileName = "Sai Test Image.jpg";
            imageSortOrder.SortOrder = 2; ;
            imageSortOrder.Image_Cloudinary_PublicId = "Test_1234";
            imageSortOrder.Image_Cloudinary_Url = "www.jll.com";
            imageSortOrder.MimeType = "jpg";
            imageSortOrder.IsActive = true;
            imageSortOrder.ModifiedOn = DateTime.Now;
            imageSortOrder.SortOrder = 1;
            imageSortOrder.ImageSortOrderId = 862;

            p.Property_ImageSortOrders.Add(imageSortOrder);

As you can see I am trying to update a child entity with Id 862. Instead it creates a new record with Id - 863. 如您所见,我正在尝试使用ID 862更新子实体。相反,它使用ID-863创建了一条新记录。

My understanding is that it should update child entities if they are already present. 我的理解是,如果子实体已经存在,它应该更新它们。 Instead it seems to be adding new records. 相反,它似乎正在添加新记录。

Can anyone tell me what is going and how do I resolve the issue. 谁能告诉我怎么回事以及如何解决该问题。

You're creating new instance of ImageSortOrder, and the Id value is assigned automatically by database. 您正在创建ImageSortOrder的新实例,并且数据库自动分配了ID值。 If you want to update existing object, you should get it from the DbContext and update it's properties. 如果要更新现有对象,则应从DbContext中获取它并更新其属性。

Alternatively, you can use Attach method on the DbSet, instead Add on child collection. 或者,您可以在DbSet上使用Attach方法,而不是在子集合上添加。 It will work like you expect. 它会像您期望的那样工作。

    var entity = new ImageSortOrder{ ImageSortOrderId = 862};
    db.ImageSortOrders.Attach(entity);

    // Now you can update the properties...

    // ...and save changes
    db.SaveChanges();

yepp and here is why i hate EF. 是的,这就是为什么我讨厌英孚。 but your code should be like this. 但是您的代码应该是这样的。

If you want a better library that do just what you said try my library EntityWorker.Core 如果您想要一个更好的库来满足您的要求,请尝试我的库EntityWorker.Core

//Property Image //属性图片

    ImageSortOrder imageSortOrder = dbcontext.Property_ImageSortOrders.Where(c=> c.ImageSortOrderId == 862).First();
    imageSortOrder.FileName = "Sai Test Image.jpg";
    imageSortOrder.SortOrder = 2; ;
    imageSortOrder.Image_Cloudinary_PublicId = "Test_1234";
    imageSortOrder.Image_Cloudinary_Url = "www.jll.com";
    imageSortOrder.MimeType = "jpg";
    imageSortOrder.IsActive = true;
    imageSortOrder.ModifiedOn = DateTime.Now;
    imageSortOrder.SortOrder = 1;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM