2015-09-06 70 views
0

我有一個文件表,其中包含一個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因爲我加一直到值的末尾,它改變了這個錯誤。

回答

1

其實我正要發佈相同的東西:)。在一對一的關係中,你不需要指定ForeignKey,因爲你的表中的關鍵字將在兩個表之間使用。所以,如果你改變你的代碼,這樣你會得到相同的結果

public class FileMap : EntityTypeConfiguration<FileContent> 
{ 
    public FileMap() 
    { 
    ToTable("FileContent"); 
    HasKey(t => t.Id); 




    WithRequired(t => t.FileModel) 
     .HasOptional(t => t.FileContent) 


    } 
} 

,或者你可以用DataAnnotations(在這裏你應該指定forgienKey。

public class FileModel 
{ 
[Key, ForeignKey("FileContent")] 
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; } 
    } 
給它
0

通過試驗和錯誤我解決了這個問題,但我無法解釋EF在我拉動時如何填充FileModel的FileContent。如果有人能解釋我會將其標記爲接受的答案。

解決方案只是從關係中刪除.Map。

 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); 


    }