我正在以下出现InvalidOperationException当我试图挽救实体有一个一对一的关系:出现InvalidOperationException由于实体框架的核心循环依赖
System.InvalidOperationException:无法保存更改因为在数据中检测到 循环依赖要保存: 'ForeignKey的: DeviceLicenseSubscriptionPlan {' LicenseId '} - > DeviceLicense {' ID '} 独特ToPrincipal:许可,ForeignKey的:DeviceLicense {' SubscriptionPlanId'} - > DeviceLicenseSubscriptionPlan {'Id'} T oPrincipal:SubscriptionPlan'。
这里是我的产品型号名称:
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; }
}
这里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);
}
我使用EF核心2.0。我可能在模型构建器中做错了什么?任何提示?
完美,谢谢! –