c# - 尝试保存更新时由于主键相同而附加实体时出错

标签 c# .net entity-framework

我正在尝试保存对现有数据库条目的更新,但是当我这样做时出现错误:

Attaching an entity of type 'FFInfo.DAL.Location' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

这是我的 Controller 的代码。我使用的保存方法与我在其他几个领域使用的相同,可以毫无问题地更新数据。

[HttpPost, ValidateAntiForgeryToken]
public ActionResult EditLocation(AddEditLocationVM model, HttpPostedFileBase MapFile)
{
    try
    {
        using (var db = new GeographyContext())
        {
            model.Sections = new SelectList(db.Sections.Where(s => s.ID > 1).OrderBy(s => s.Title), "ID", "Title").ToList();
            model.GeographyTypes = new SelectList(db.GeographyTypes.Where(gt => gt.SectionID == model.Section).OrderBy(gt => gt.Name), "ID", "Name").ToList();
            model.ParentLocations = new SelectList(db.Locations.Where(l => l.SectionID == model.Section).OrderBy(l => l.Name), "ID", "Name").ToList();


            if (MapFile != null)
            {
                if (FileHelper.IsNotValidImage(MapFile))
                {
                    ModelState.AddModelError("Invaalid File Type", "Images must be JPG, GIF, or PNG files.");
                }
            }

            if (ModelState.IsValid)
            {
                if (MapFile != null)
                {
                    var SectionRoute = db.Sections.Where(s => s.ID == model.Section).Select(s => s.Route).First();
                    model.MapFileID = FileHelper.UploadFile("Images/" + SectionRoute + "/Maps/" + MapFile.FileName.ToList(), "site", MapFile);
                }

                if (model.ParentLocation == 0)
                {
                    model.ParentLocation = null;
                }

                var UpdatedLocation = new Location()
                {
                    Description = model.Description,
                    GeographyTypeID = model.GeographyType,
                    ID = model.ID,
                    MapFileID = model.MapFileID,
                    Name = model.Name,
                    ParentLocationID = model.ParentLocation,
                    SectionID = model.Section
                };

                db.Entry(UpdatedLocation).State = EntityState.Modified;
                db.SaveChanges();
                ViewBag.Results = "Location information updated.";
            }

            return View(model);
        }
    }
    catch (Exception ex)
    {
        ErrorSignal.FromCurrentContext().Raise(ex);
        model.Sections = Enumerable.Empty<SelectListItem>();
        ViewBag.Results = "Error updating location informaiton, please try again later.";
        return View(model);
    }
}

这是我的位置实体代码:

public class Location
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required, Index("IX_Location", 1, IsUnique = true)]
    public string Name { get; set; }

    [Index("IX_Location", 2, IsUnique = true)]
    public Int16 SectionID { get; set; }

    [Column(TypeName = "varchar(MAX)")]
    public string Description { get; set; }

    public Int16 GeographyTypeID { get; set; }
    public int? MapFileID { get; set; }
    public int? ParentLocationID { get; set; }

    [ForeignKey("SectionID")]
    public Section Section { get; set; }

    [ForeignKey("GeographyTypeID")]
    public GeographyType GeographyType { get; set; }

    [ForeignKey("MapFileID")]
    public File Map { get; set; }

    [ForeignKey("ParentLocationID")]
    public Location ParentLocation { get; set; }

    public ICollection<LocationTransitionPoint> TransitionPoints { get; set; }
}

这是我第一次尝试更新这样一个更复杂的实体,但根据我在网上找到的内容,我看不出有什么问题。

最佳答案

在 Entity Framework 的内存中不能有两个具有相同主键的实体(相同类型)。

问题是

model.ParentLocations = new SelectList(db.Locations.Where(l => l.SectionID == model.Section).OrderBy(l => l.Name), "ID", "Name").ToList();

在上一行中,您以某种方式加载了 Location,其 IDmodel.ID

然后在

var UpdatedLocation = new Location()
{
    Description = model.Description,
    GeographyTypeID = model.GeographyType,
    ID = model.ID,
    MapFileID = model.MapFileID,
    Name = model.Name,
    ParentLocationID = model.ParentLocation,
    SectionID = model.Section
};
db.Entry(UpdatedLocation).State = EntityState.Modified;

您正在创建一个新的 Location 并尝试将其附加到上下文(通过将其状态设置为已修改),但您已经加载了另一个具有确切主键的 Location 实体作为 UpdatedLocation 进入内存中的某处,这会导致异常。

尝试获取位置,然后更改属性。

var UpdateLocation = db.Locations.First(l => l.ID == model.ID);
// var UpdateLocation = db.Locations.Find(model.ID); maybe a better option
UpdatedLocation.Description = model.Description;
UpdatedLocation.GeographyTypeID = model.GeographyType;
UpdatedLocation.MapFileID = model.MapFileID;
UpdatedLocation.Name = model.Name;
UpdatedLocation.ParentLocationID = model.ParentLocation;
UpdatedLocation.SectionID = model.Section;

关于c# - 尝试保存更新时由于主键相同而附加实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541131/

相关文章:

c# 如何在 DataGrid 中正确对齐数字

c# - NLog 不使用 ClickOnce 进行记录

c# - TPL 和内存管理

c# - DbContext 处理?

entity-framework - Entity Framework 代码优先默认数据库位置

c# - 如何在第三个项目中引用和使用 .NET Core 3.0 和 .NET EF 4.7 项目,并使用它们的数据库连接?

c# - 找不到按钮调用的方法

c# - KnockOut.js 与 Asp.net mvc

c# - 最新的 Resharper 更新后无法编辑文件

c# - DynamoDB .NET - 从表中删除所有项目