2017-04-26 152 views
0

我有一個用戶模型和定義實體框架的關係

public class User 
{ 
    public string UserID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string FullName { 
     get 
     { 
      return (FirstName + " " + LastName).Trim(); 
     } 
    } 
    public string EmailID { get; set; } 
    public UserAccessLabel AccessLabel { get; set; } 
    public string Password { get; set; } 
    public string PasswordStore { 
     get 
     { 
      string hashPass; 
      using (Encryption enc = new Encryption()) 
      { 
       enc.Key = EncryptionKey.DefaultKey; 
       hashPass = enc.Encrypt(Password); 
      } 
      return hashPass; 
     } 
     set 
     { 
      string hashPass; 
      using (Encryption enc = new Encryption()) 
      { 
       enc.Key = EncryptionKey.DefaultKey; 
       hashPass = enc.Decrypt(value); 
      } 
      Password = hashPass; 
     } 
    } 
    public byte[] Image { get; set;} 
} 

和fluentAPI配置類是

public class UserConfig : EntityTypeConfiguration<User> 
{ 
    public UserConfig() 
    { 
     ToTable("tblUsers"); 
     HasKey(k => k.UserID); 
     Property(p => p.FirstName).IsRequired(); 
     Property(p => p.LastName).IsRequired(); 
     Property(p => p.PasswordStore).IsRequired().HasColumnName("Password"); 
     Property(p => p.Image).HasColumnType("image"); 
     Ignore(i => i.FullName); 
     Ignore(i => i.Password); 
    } 
} 

,我有很多的同類產品,類別,客戶等和其他模型每種模式都有創建更新屬性,它是用戶。

而我的問題是,我是否必須創建所有模型集合屬性用戶模型?

public ICollection<Product> Products { get; set; } 



public class ProductConfig:EntityTypeConfiguration<Product> 
{ 
    public ProductConfig() 
    { 
     ToTable("tblProducts"); 
     HasKey(k => k.Code); 
     Property(p => p.Title).IsRequired(); 
     Property(p => p.Title).HasMaxLength(100); 


     HasRequired(c => c.Category).WithMany(p => p.Products).HasForeignKey(f => f.CategoryID).WillCascadeOnDelete(false); 
     HasRequired(u => u.Created).WithMany(p => p.Products).HasForeignKey(f => f.CreatedUser).WillCascadeOnDelete(false); 
     HasRequired(u => u.Updated).WithMany(p => p.Products).HasForeignKey(f => f.UpdatedUser).WillCascadeOnDelete(false); 
    } 
} 

在此先感謝

回答

0

如果您不需要這些藏品,你不必。在這些實體上具有FK屬性並且在DbContext中具有相應的DbSet屬性就足夠了。

更新

如果你的意思是,FK id字段是不是空的,但對象字段是null,這是因爲他們沒有被默認加載。你可以通過調用擴展方法Include這樣

context.Products.Include("Created") 
+0

但我不能夠從外鍵列訪問數據,存在於數據庫中的數據時,我通過使用簡單的SQL查詢像Created_UserID檢查,但在模型,可以根據產品型號顯示在已創建的屬性無效 – Siraj

+0

如果您的意思是FK id列不是空的,但對象字段爲空,這是因爲它們並未默認加載。你可以通過調用擴展方法'Include'這樣 'context.Products.Include(「創建」)加載它們' – Random

+0

是啊,這是我一直在尋找的答案。好的,非常感謝 – Siraj

0

我認爲你正在嘗試做的是使一個在代碼中第一個數據庫一對多的關係。 public ICollection<Product> Products { get; set; }是使各user許多products所有者正確的語法。然後在產品模型文件中,您將包含一個外鍵給它的用戶。

public class Product 
{ 
    public virtual User ProductsUser {get; set;} 
} 

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

+0

這意味着我必須創造這樣 加載它們 ' 公衆的ICollection 產品{獲得;組; } public ICollection Vendors {get;組; } public ICollection Customers {get;組; } ' 在用戶模式 – Siraj