2017-04-26 188 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