我有一个用户模型和定义实体框架的关系
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);
}
}
在此先感谢
但我不能够从外键列访问数据,存在于数据库中的数据时,我通过使用简单的SQL查询像Created_UserID检查,但在模型,可以根据产品型号显示在已创建的属性无效 – Siraj
如果您的意思是FK id列不是空的,但对象字段为空,这是因为它们并未默认加载。你可以通过调用扩展方法'Include'这样 'context.Products.Include(“创建”)加载它们' – Random
是啊,这是我一直在寻找的答案。好的,非常感谢 – Siraj