简体   繁体   English

由于Entity Framework Core中的循环依赖关系,导致InvalidOperationException

[英]InvalidOperationException due to circular dependency in Entity Framework Core

I am getting the following InvalidOperationException when I try to save an entity with a one-to-one relationship: 当我尝试保存具有一对一关系的实体时,出现以下InvalidOperationException异常

System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved: 'ForeignKey: DeviceLicenseSubscriptionPlan {'LicenseId'} -> DeviceLicense {'Id'} Unique ToPrincipal: License, ForeignKey: DeviceLicense {'SubscriptionPlanId'} -> DeviceLicenseSubscriptionPlan {'Id'} ToPrincipal: SubscriptionPlan'. System.InvalidOperationException:无法保存更改,因为在要保存的数据中检测到循环依赖项:'ForeignKey:DeviceLicenseSubscriptionPlan {'LicenseId'}-> DeviceLicense {'Id'}唯一ToPrincipal:许可证,ForeignKey:DeviceLicense {'SubscriptionPlanId' }-> DeviceLicenseSubscriptionPlan {'Id'} ToPrincipal:SubscriptionPlan'。

Here is my modell: 这是我的模型:

public class DeviceLicense
{
    public Guid? Id { get; set; }
    public int DeviceLimit { get; set; }
    public DeviceLicenseSubscriptionPlan SubscriptionPlan { get; set; } = new DeviceLicenseSubscriptionPlan();
}

public class DeviceLicenseSubscriptionPlan 
{
    public Guid? Id { get; set; }
    public Guid? LicenseId { get; set; }
    public DeviceLicense License { get; set; }
}

Here the OnModelCreating() : 这是OnModelCreating()

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var deviceLicense = modelBuilder.Entity<DeviceLicense>().ToTable("DeviceLicense");
    deviceLicense.HasKey(l => l.Id);
    deviceLicense.HasOne<DeviceLicenseSubscriptionPlan>()
        .WithOne(s => s.License)
        .HasForeignKey<DeviceLicenseSubscriptionPlan>(s => s.LicenseId)
        .HasConstraintName("LicenseId");
    deviceLicense.Property(l => l.DeviceLimit).HasColumnName("DeviceLimit");

    var deviceLicenseSubPlan = modelBuilder.Entity<DeviceLicenseSubscriptionPlan>().ToTable("DeviceLicenseSubscriptionPlan");
    deviceLicenseSubPlan.HasKey(s => s.Id);
    deviceLicenseSubPlan.Property(s => s.Id).HasColumnName("SubscriptionPlanId");


    base.OnModelCreating(modelBuilder);
}

I am using EF Core 2.0. 我正在使用EF Core 2.0。 I probably do something wrong within the ModelBuilder? 我可能在ModelBuilder中做错了什么? Any hints? 有什么提示吗?

The problem is this line 问题是这条线

deviceLicense.HasOne<DeviceLicenseSubscriptionPlan>()

It basically tells EF that there is no navigation property to DeviceLicenseSubscriptionPlan in DeviceLicense . 它主要是告诉EF没有导航属性DeviceLicenseSubscriptionPlanDeviceLicense However there is a navigation property , so EF by convention maps it to a second relationship with FK in DeviceLicense pointing to DeviceLicenseSubscriptionPlan . 但是, 有一个导航属性 ,因此EF按照惯例将其映射到与FK在第二个关系DeviceLicense指向DeviceLicenseSubscriptionPlan Which of course with the combination of the desired FK in DeviceLicenseSubscriptionPlan creates a cycle. 当然,哪个与DeviceLicenseSubscriptionPlan所需FK的组合一起创建了一个循环。

Make sure fluent configuration uses the correct overloads which exactly represent the presence/absence of a navigation property in either side of the relationship. 确保流畅的配置使用正确的重载,这些重载准确表示关系的任一侧是否存在导航属性。 In this particular case, replace the above with 在这种情况下,将以上内容替换为

deviceLicense.HasOne(l => l.SubscriptionPlan)

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

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