1
請幫助查找錯誤。EF4映射一對多
錯誤文本:在模型生成過程中檢測到一個或多個驗證錯誤。因爲所有在從屬角色的屬性是不可爲空,主要角色的多重性必須是1
//File.cs
public class File
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Sp> Sps { get; set; }
}
//Sp.cs
public class Sp
{
public int Id { get; set; }
public int FileId { get; set; }
public File File { get; set; }
}
映射代碼
//mapping File
class File : EntityTypeConfiguration<File>
{
public File()
{
ToTable("File", "dbo");
HasKey(x => x.Id);
Property(x => x.Id).HasColumnName("ID");
Property(x => x.Name).HasColumnName("NAME");
}
}
//mapping Sp
class Sp : EntityTypeConfiguration<Sp>
{
public Sp()
{
ToTable("Sp", "dbo");
HasKey(x => x.Id);
Property(x => x.Id).HasColumnName("ID");
Property(x => x.FileId).HasColumnName("FILE_ID");
//the location of the error
HasOptional(d => d.File)
.WithMany(d => d.Sps)
.HasForeingKey(d => d.FileId)
.WillCascadeOnDelete(false);
}
}
http://stackoverflow.com/questions/17420064/problems-creating-a-foreign-key-relationship-on-entity-framework – artm 2014-10-27 11:18:21
你的外鍵不能是可選的,沒有一個可選的值(即可空屬性),刪除。 – DavidG 2014-10-27 11:19:34