我有一個文件表,其中包含一個URL在互聯網上的某個文件或它需要是一個物理文件存儲到一個varbinary(max)
列。我創建了第二個表來存儲實際的varbinary(max)
,但是我從未映射過一對一,並且在我嘗試保存時向我發送的錯誤是FileContentId
無效。C#實體框架:可選一對一關係
這是我的模型和地圖:
public class FileModel
{
public string Id { get; set; }
public string Name { get; set; }
public string FileContentId { get; set; }
public string Url { get; set; }
public string CreatedById { get; set; }
public DateTime CreatedOn { get; set; }
public virtual ICollection<FileTable> FileTables { get; set; }
public virtual User CreatedBy { get; set; }
public virtual FileContent FileContent { get; set; }
}
public class FileContent
{
public string Id { get; set; }
public byte[] Contents { get; set; }
public virtual FileModel FileModel { get; set; }
}
public class FileMap : EntityTypeConfiguration<FileModel>
{
public FileMap()
{
ToTable("Files");
HasKey(t => t.Id);
Property(t => t.Id);
Property(t => t.Name);
Property(t => t.FileContentId);
Property(t => t.Url);
Property(t => t.CreatedById);
Property(t => t.CreatedOn);
// Relationships
HasRequired(t => t.CreatedBy)
.WithMany(t => t.Files)
.HasForeignKey(t => t.CreatedById);
HasOptional(t => t.FileContent)
.WithRequired(t => t.FileModel)
.Map(t => t.MapKey("FileContentId"));
}
}
拋出的錯誤是
無效的列名稱FileContentId「
我敢肯定這是.Map
因爲我加一直到值的末尾,它改變了這個錯誤。