EF CodeFirst - 无法创建数据库
EF CodeFirst - cannot create database
我在尝试 运行 我的 MVC 应用程序时遇到错误
Introducing FOREIGN KEY constraint 'FK_dbo.Passages_dbo.Localizations_ToID' on table 'Passages' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors'
我看过很多帖子,但我不知道我现在应该做什么。
有我的模型:
public class Passage
{
[Key]
public int ID { get; set; }
public int FromID { get; set; }
[ForeignKey("FromID")]
public Localization FromLocalizaton { get; set; }
public int ToID { get; set; }
[ForeignKey("ToID")]
public Localization ToLocalization { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime ArrivalTime { get; set; }
public DateTime? AdditionalTime { get; set; }
public bool Weekend { get; set; }
public int Seats { get; set; }
}
public class Localization
{
[Key]
public int ID { get; set; }
public string Province { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string StreetAdres { get; set; }
}
Passage有两个外键指的是一对一关系的Lozalization
问题出自:
Passage has two foreign key refers to Lozalization with one to one relationship
因为默认情况下 Passage
中需要这两个关系(查看外键 FromID
和 ToID
没有 Nullable<int>
或 int?
)因此 Code First 对这些关系创建级联删除操作。然而,两次级联删除将应用于同一个 table,这是不允许的。
要解决此问题,您有两种解决方案:
创建一个外键 属性 Nullable<int>
,默认情况下不会对该关系创建级联删除操作。
或者您可以像这样使用 Fluent API 禁用级联删除操作:
// Assuming that you want to disable cascade deletion with ToLocalization
modelBuilder.Entity<Passage>()
.HasRequired(p => p.ToLocalization)
.WithMany()
.WillCascadeOnDelete(false);
我在尝试 运行 我的 MVC 应用程序时遇到错误
Introducing FOREIGN KEY constraint 'FK_dbo.Passages_dbo.Localizations_ToID' on table 'Passages' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors'
我看过很多帖子,但我不知道我现在应该做什么。 有我的模型:
public class Passage
{
[Key]
public int ID { get; set; }
public int FromID { get; set; }
[ForeignKey("FromID")]
public Localization FromLocalizaton { get; set; }
public int ToID { get; set; }
[ForeignKey("ToID")]
public Localization ToLocalization { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime ArrivalTime { get; set; }
public DateTime? AdditionalTime { get; set; }
public bool Weekend { get; set; }
public int Seats { get; set; }
}
public class Localization
{
[Key]
public int ID { get; set; }
public string Province { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string StreetAdres { get; set; }
}
Passage有两个外键指的是一对一关系的Lozalization
问题出自:
Passage has two foreign key refers to Lozalization with one to one relationship
因为默认情况下 Passage
中需要这两个关系(查看外键 FromID
和 ToID
没有 Nullable<int>
或 int?
)因此 Code First 对这些关系创建级联删除操作。然而,两次级联删除将应用于同一个 table,这是不允许的。
要解决此问题,您有两种解决方案:
创建一个外键 属性 Nullable<int>
,默认情况下不会对该关系创建级联删除操作。
或者您可以像这样使用 Fluent API 禁用级联删除操作:
// Assuming that you want to disable cascade deletion with ToLocalization
modelBuilder.Entity<Passage>()
.HasRequired(p => p.ToLocalization)
.WithMany()
.WillCascadeOnDelete(false);