【问题标题】:EF Core does not track entity correctlyEF Core 无法正确跟踪实体
【发布时间】:2024-05-19 03:10:01
【问题描述】:

您好,我对 EF Core 插入实体有疑问。问题是我需要插入与另一个已经存在的实体相关的新实体。我已经创建了与 fluent API 的关系。我已经这样做了两次。首先,我正在创建汽车并使用身份用户添加最后编辑的字段,并且所有工作都可以,但是当我尝试对另一个实体执行相同操作时,它会崩溃

我的流利的 APi 代码运行良好:

 builder.Entity<Car>()
            .HasOne(x => x.Owner)
            .WithMany(x => x.OwnerCars)
            .HasForeignKey(x => x.OwnerId);

这里是汽车实体:

public class Car : CarBase
{
    [Key]
    public int CarId { get; set; }
    public bool IsTrailer { get; set; }
    public virtual TrailerType TrailerType { get; set; }
    public virtual int? TrailerTypeId { get; set; }
    public virtual ApplicationUser Owner { get; set; }
    public virtual string OwnerId { get; set; }
}

这里是应用程序用户实体

public class ApplicationUser : IdentityUser
{
    [MaxLength(100)]
    public string Address { get; set; }
    public DateTime CreatedDateTime { get; set; }
    public DateTime LastEditationDateTime { get; set; }
    public virtual ApplicationUser LastEditedBy { get; set; }
    public bool IsDeleted { get; set; }
    public virtual DateTime DeletedDateTime { get; set; }
    public ICollection<DriverLicenseApplicationUser> DriverLicenses { get; set; }
    public ICollection<RideApplicationUser> Rides { get; set; }
    public ICollection<Car> OwnerCars { get; set; }
    public ICollection<Car> EditedCars { get; set; }
    public ICollection<Trailer> EditedTrailers { get; set; }
    public ICollection<Customer> EditedCustomers { get; set; }

}

要添加这个实体,我只调用这个函数就可以了。

public Car CreateCar(Car car)
    {
        _context.Cars.Add(car);
        return car;
    }

但是当我想以这种方式保存另一种实体类型时,它会显示错误。所有步骤都相同,所以我不明白这一点。在这里,我添加了我用来执行此操作的代码。

builder.Entity<Trailer>()
            .HasOne(x => x.TrailerType)
            .WithMany(x => x.Trailers)
            .HasForeignKey(x => x.TrailerTypeId);

这是预告片:

public class Trailer : CarBase
{
    [Key]
    public int TrailerId { get; set; }
    //[Required]
    public virtual TrailerType TrailerType { get; set; }
    public virtual int TrailerTypeId { get; set; }
}

这里是traylerTyper:

 public class TrailerType:Trackable
{
    //[Key]
    public int TrailerTypeId { get; set; }
    [MaxLength(100)]
    [Required]
    public string Type { get; set; }
    public string Note { get; set; }
    public ICollection<Car> TrailerTypeCars { get; set; }
    public ICollection<Trailer> Trailers{ get; set; }
}

方法和前面提到的一样

public Trailer CreateTrailer(Trailer trailer)
    {
        trailer.TrailerTypeId = trailer.TrailerType.TrailerTypeId;

        //_context.Attach(trailer.TrailerType);
        var result = _context.Trailers.Add(trailer);
        return result.Entity;
    }

当我取消注释附加时,它可以工作,但我认为我不必附加这个,因为我已经获得了基于 ID 的关系,并且首先提到的示例效果很好。这让我毫无意义。所以如果有人能给我建议,那就太棒了。

这是我得到的错误:

Cannot insert explicit value for identity column in table 'TrailerTypes' when IDENTITY_INSERT is set to OFF.

看起来 EF 不知道托盘类型实体已经存在,并试图再次插入相同的实体并且应用程序崩溃,因为它已经存在并且我不允许直接插入 ID。正如我所说,我完全不知道为什么会发生这种情况。

【问题讨论】:

    标签: c# asp.net entity-framework entity-framework-core


    【解决方案1】:

    问题是延迟加载。 ViewModel 中的属性与数据库中的属性并不完全相同,EF 跟踪对象中属性的整个图并且不识别它是同一个对象。解决方案是只使用 ID 而不是整个对象。

    【讨论】:

      最近更新 更多