Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架关系_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 实体框架关系

C# 实体框架关系,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我有一个用户模型,定义是 public class User { public string UserID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return (FirstName + " " + LastNa

我有一个用户模型,定义是

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类UserConfig:EntityTypeConfiguration
{
公共用户配置()
{
ToTable(“tblUsers”);
HasKey(k=>k.UserID);
属性(p=>p.FirstName).IsRequired();
属性(p=>p.LastName).IsRequired();
属性(p=>p.PasswordStore).IsRequired().HasColumnName(“密码”);
属性(p=>p.Image);
忽略(i=>i.FullName);
忽略(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);
    }
}
公共ICollection产品{get;set;}
公共类ProductConfig:EntityTypeConfiguration
{
公共产品配置()
{
ToTable(“TBL产品”);
HasKey(k=>k.Code);
属性(p=>p.Title).IsRequired();
属性(p=>p.Title).HasMaxLength(100);
HasRequired(c=>c.Category)。有许多(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)。带有多个(p=>p.Products)。HasForeignKey(f=>f.updateUser)。WillCascadeOnDelete(false);
}
}

提前感谢

我认为您要做的是在代码优先数据库中建立一对多关系
public-ICollection-Products{get;set;}
是使每个
用户成为许多
产品的所有者的正确语法。然后在产品模型文件中,您将为其用户包含一个外键

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

如果您不需要这些收藏,您不必这样做。在这些实体上具有FK属性并在DbContext中具有相应的DbSet属性就足够了

更新


如果您的意思是FK id列不为空,但对象字段为
null
,这是因为默认情况下未加载它们。您可以像这样调用扩展方法
Include
来加载它们

context.Products.Include("Created")

这意味着我必须在用户模型中创建类似“public ICollection Products{get;set;}public ICollection Vendors{get;set;}public ICollection Customers{get;set;}”的内容,但我无法从外键列访问数据,当我像Created_UserID一样使用简单的sql查询检查数据库时,数据存在于数据库中,但在模型中,它在产品模型下的Created属性中显示null如果您的意思是FK id列不是空的,但对象字段是空的,这是因为默认情况下没有加载它们。您可以通过调用扩展方法
Include
加载它们,就像这样
context.Products.Include(“Created”)
是的,这就是我想要的答案。“非常感谢你,”他补充道