当我尝试使用 CodeFirst 创建我的数据库时,出现错误
在表 'Location' 上引入 FOREIGN KEY 约束 'FK_dbo.Location_dbo.Section_SectionID' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
看着这两张表,我不明白为什么会弹出这个错误,谁能告诉我我错过了什么?
节表:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FFInfo.DAL
{
[Table("Section")]
public class Section
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int16 ID { get; set; }
public Int64? LogoFileID {get;set;}
[Required, MaxLength(250), Column(TypeName = "varchar")]
public string RouteName { get; set; }
[Required, MaxLength(15), Column(TypeName = "varchar")]
public string Type { get; set; }
[ForeignKey("LogoFileID")]
public virtual File Logo { get; set; }
public virtual ICollection<SectionTranslation> Translations { get; set; }
}
}
位置表:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FFInfo.DAL
{
[Table("Location")]
public class Location
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required]
public Int16 SectionID { get; set; }
[Required]
public byte LocationTypeID { get; set; }
public Int64? MapID { get; set; }
[ForeignKey("SectionID")]
public virtual Section Section { get; set; }
[ForeignKey("LocationTypeID")]
public virtual LocationType LocationType { get; set; }
}
}
位置翻译:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FFInfo.DAL
{
[Table("LocationTypeTranslation")]
public class LocationTypeTranslation
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required]
public byte TypeID { get; set; }
[Required]
public byte CultureCodeID { get; set; }
[Required]
public string Name { get; set; }
[ForeignKey("TypeID")]
public virtual LocationType Type { get; set; }
[ForeignKey("CultureCodeID")]
public virtual CultureCode CultureCode { get; set; }
}
}
部分翻译:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FFInfo.DAL
{
[Table("SectionTranslation")]
public class SectionTranslation
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required]
public Int16 SectionID { get; set; }
[Required]
public byte CultureCodeID { get; set; }
[Required]
public string Title { get; set; }
public string Synopsis { get; set; }
[ForeignKey("SectionID")]
public Section Section { get; set; }
[ForeignKey("CultureCodeID")]
public virtual CultureCode CultureCode { get; set; }
}
}
文化代码:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FFInfo.DAL
{
[Table("CultureCode")]
public class CultureCode
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte ID { get; set; }
[Required, MaxLength(5), Column(TypeName = "char")]
public string Code { get; set; }
[Required, MaxLength(50), Column(TypeName = "varchar")]
public string LanguageName { get; set; }
[Required, MaxLength(50)]
public string DisplayName { get; set; }
}
}