2017-04-25 33 views
1

我奮力開創實體框架的關係三個表之間.. 我使用C#開發WPF應用程序,SQLite的3.0和Entity Framework 6的EntityFramework 1至0,1或一對多的關係

我有以下表(類):

  1. 投資者
  2. 投資
  3. ImageStore

我想創建一個模型,以便投資者和投資人(以及其他未來的其他類)可以存儲0,1或多個圖像..(因爲從類名顯而易見,投資者只能擁有一個配置文件圖像並且投資類可以擁有多張圖片) 我ImageStore類看起來是這樣的:

public class ImageStore : PropertyChangedNotification 
    { 
     [Key] 
     public int ImageStoreId { get; set; } 
     [Required] 
     public string ImageFile { get; set; } 
     [Required] 
     public Byte[] ImageBlob { get; set; } 

     public string FileName { get; set; } 
     [Required] 
     public int FileSize { get; set; } 


     //public virtual ImageData ImageData { get; set; } 
    } 

爲了創建1至0,1或一對多的關係,我創建了另一個名爲中間表:的ImageData,如下圖所示(我不知道知道它是否真的是一個很好的做法,但我現在能想到的只有..)

public class ImageData : PropertyChangedNotification 
    { 
     [Key] 
     public int ImageDataId { get; set; } 

     [ForeignKey("Investment")] 
     public long? InvestmentId { get; set; } 

     [ForeignKey("Investor")] 
     public long? InvestorId { get; set; } 

     [ForeignKey("ImageStore")] 
     public int ImageStoreId { get; set; } 

     public virtual ImageStore ImageStore { get; set; } 

     public virtual Investment Investment { get; set; } 

     public virtual Investor Investor { get; set; } 
    } 

我的投資類看起來是這樣的:

public class Investor : PropertyChangedNotification 
    { 
     [Key] 
     public long InvestorId { get; set; }   

     [NotMapped] 
     [ForeignKey("ImageData")] 
     public List<int> ImageDataList { get; set; } 

     public virtual ICollection<ImageData> ImageDataCollection { get; set; } 
     public virtual ICollection<Investment> Investments { get; set; }   
    } 

我的投資類如下所示:

public class Investment : PropertyChangedNotification 
    { 
     [Key] 
     public long InvestmentId { get; set; } 

     [ForeignKey("Investor")] 
     [Required] 
     public long FirstInvestorId { get; set; }  


     [NotMapped] 
     [ForeignKey("ImageData")] 
     public List<int> ImageDataList { get; set; } 

     public virtual ICollection<ImageData> ImageDataCollection { get; set; }  

     public virtual Investor Investor { get; set; } 

     [NotMapped] 
     [Required (ErrorMessage = "First Investor is Required")] 
     public Investor FirstInvestor { get; set; } 
    } 

這是我與流暢的配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      // MyData Database does not pluralize table names 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

      modelBuilder.Entity<Investor>().HasOptional(s => s.ImageDataCollection); 
      modelBuilder.Entity<Investment>().HasOptional(s => s.ImageDataCollection); 
      //modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 
     } 

當我開始調試應用程序,我收到以下錯誤:

  1. ImageData_Investment_Source::多重性在'ImageData_Investment'關係中的角色'ImageData_Investment_Source'中無效。因爲依賴角色屬性不是關鍵屬性,所以從屬角色的多重性的上界必須是'*'
  2. ImageData_Investor_Source::多重性在'ImageData_Investor'關係中的角色'ImageData_Investor_Source'中無效。由於依賴性角色屬性不是關鍵屬性,所以從屬角色的多重性的上限必須爲'*'

有人可以請我提出解決此問題的方法和/或實現最佳方法我需要什麼,我真的很感激。 由於

+0

嘗試改變''INT在FKS爲''長(或'long'中的PK到'int') –

+0

後?使得在課堂上的變化(O狂InvestoerId和InvestmentId中的ImageData類是長),我的第二個和第四個錯誤,走了,現在我只剩下錯誤#1和3 –

+0

你有相關的這裏沒有顯示流利的配置? –

回答

2

的流利配置

modelBuilder.Entity<Investor>().HasOptional(s => s.ImageDataCollection); 
modelBuilder.Entity<Investment>().HasOptional(s => s.ImageDataCollection); 

是不完整的。

由於您已經擁有必要的數據註釋和導航/ FK屬性,因此您可以簡單地將其刪除。或者,如果你想提供流暢的配置(這是我個人比較喜歡,因爲它可以讓你明確指定的一切,而不是依靠公約和專門針對的關係,不是那麼直觀ForegnKeyInverseProperty數據註解),那麼你應該確保它反映完全在相關實體中存在導航和FK屬性。

正確流利的配置反映了你的模型,到目前爲止是這樣的:

modelBuilder.Entity<Investor>() 
    .HasMany(e => e.ImageDataCollection) 
    .WithOptional(e => e.Investor) 
    .HasForeignKey(e => e.InvestorId); 

modelBuilder.Entity<Investment>() 
    .HasMany(e => e.ImageDataCollection) 
    .WithOptional(e => e.Investment) 
    .HasForeignKey(e => e.InvestmentId);