[英]EntityFramework Core Fluent API
I have two tables: EstimationActivity and Activity: 我有两个表:EstimationActivity和Activity:
public class EstimationActivity
{
public Guid Id { get; set; }
public Guid EstimateId { get; set; }
public virtual Estimate Estimate { get; set; }
public Guid ParentActivityId { get; set; }
public virtual Activity ParentActivity { get; set; }
public Guid ActivityId { get; set; }
public virtual Activity Activity { get; set; }
public double Duration { get; set; }
[Range(minimum: 0, maximum: 100)]
public int Contingency { get; set; }
public ICollection<Note> Notes { get; set; } = new List<Note>();
}
public class Activity
{
public Guid Id { get; set; }
public Guid ActivityTypeId { get; set; }
public virtual ActivityType ActivityType { get; set; }
public Guid RfcAreaId { get; set; }
public virtual RfcArea RfcArea { get; set; }
[Required]
[StringLength(maximumLength: 60, MinimumLength = 5)]
public string Name { get; set; }
}
As you can see there are two foreign keys to the Activity table(class). 如您所见,Activity表(类)有两个外键。 When I try to add a migration I get:
当我尝试添加迁移时,我得到:
Introducing FOREIGN KEY constraint 'FK_EstimateActivities_Activities_ParentActivityId' on table 'EstimateActivities' may cause cycles or multiple cascade paths.
在表“ EstimateActivities”上引入FOREIGN KEY约束“ FK_EstimateActivities_Activities_ParentActivityId”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
I tried to restrict all cascade deletions with this, as I found in few articles: 正如我在几篇文章中发现的那样,我试图以此来限制所有级联删除:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
This didn't do the work. 这没做。 How should I go over the error?
我应该如何解决该错误?
Try use attributes [ForeignKey("SomeId")]
where you set FK's and [InverseProperty("OtherClass")]
overrides this convention and specifies alignment of the properties. 尝试使用属性
[ForeignKey("SomeId")]
在设置FK的位置,而[InverseProperty("OtherClass")]
会覆盖此约定并指定属性的对齐方式。
public class EstimationActivity
{
public Guid Id { get; set; }
public Guid EstimateId { get; set; }
public virtual Estimate Estimate { get; set; }
public Guid ParentActivityId { get; set; }
[ForeignKey("ParentActivityId")]
public virtual Activity ParentActivity { get; set; }
public Guid ActivityId { get; set; }
[ForeignKey("ActivityId")]
public virtual Activity Activity { get; set; }
public double Duration { get; set; }
[Range(minimum: 0, maximum: 100)]
public int Contingency { get; set; }
public ICollection<Note> Notes { get; set; } = new List<Note>();
}
public class Activity
{
public Guid Id { get; set; }
public Guid ActivityTypeId { get; set; }
public virtual ActivityType ActivityType { get; set; }
public Guid RfcAreaId { get; set; }
public virtual RfcArea RfcArea { get; set; }
[Required]
[StringLength(maximumLength: 60, MinimumLength = 5)]
public string Name { get; set; }
[InverseProperty("ParentActivity")]
public virtual EstimationActivity EstimationParentActivity { get; set; }
[InverseProperty("Activity")]
public virtual EstimationActivity EstimationActivity { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.