Entity framework 实体框架、读写配置

Entity framework 实体框架、读写配置,entity-framework,entity-framework-6,Entity Framework,Entity Framework 6,给定以下数据库模型 我有两个fluent实体框架配置,一个在我读取时工作(设置了setupFields列表),另一个用于写入,如果我也用于读取,则总是带有一个空的setupFields列表 [Table("[BALANCE.SETUP]")] public class SetupEntity { [Key] public Guid Id { get; set; } public string Title { get; set; } public virtua

给定以下数据库模型

我有两个fluent实体框架配置,一个在我读取时工作(设置了setupFields列表),另一个用于写入,如果我也用于读取,则总是带有一个空的setupFields列表

[Table("[BALANCE.SETUP]")]
public class SetupEntity
{
    [Key]
    public Guid Id { get; set; }

    public string Title { get; set; }

    public virtual ChassisEntity Chassis { get; set; }

    public virtual EventEntity Event { get; set; }

    public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}

[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
    [Key]
    [Column(Order = 0)]
    public Guid SetupId { get; set; }

    [Key]
    [Column(Order = 1)]
    public int Sequence { get; set; }

    public string Section { get; set; }

    public string Name { get; set; }

    public string Value { get; set; }

    public virtual SetupEntity Setup { get; set; }
}
更新2

根据下面的注释,我将映射更改为只有以下内容:插入时有效,但读取子集合时仍然为空:

modelBuilder.Entity<SetupEntity>().HasMany(x => x.SetupFields);


[Table("[BALANCE.SETUP]")]
public class SetupEntity
{
    [Key]
    public Guid Id { get; set; }

    public string Title { get; set; }

    public virtual ChassisEntity Chassis { get; set; }

    public virtual EventEntity Event { get; set; }

    public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}

[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
    [Key]
    [Column(Order = 0)]
    public Guid SetupId { get; set; }

    [Key]
    [Column(Order = 1)]
    public int Sequence { get; set; }

    public string Section { get; set; }

    public string Name { get; set; }

    public string Value { get; set; }

    public virtual SetupEntity Setup { get; set; }
}
modelBuilder.Entity().HasMany(x=>x.SetupFields);
[表(“[BALANCE.SETUP]”)
公共类设置实体
{
[关键]
公共Guid Id{get;set;}
公共字符串标题{get;set;}
公共虚拟机箱实体机箱{get;set;}
公共虚拟事件实体事件{get;set;}
公共虚拟ICollection设置字段{get;set;}
}
[表(“[BALANCE.SETUP.FIELD]”)
公共类SetupFieldEntity
{
[关键]
[第列(顺序=0)]
公共Guid SetupId{get;set;}
[关键]
[第列(顺序=1)]
公共整数序列{get;set;}
公共字符串部分{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共虚拟设置实体设置{get;set;}
}
更新3

根据注释,我完全删除了实体中的属性,但集合属性仍然为空:(

公共类设置实体
{
公共Guid Id{get;set;}
公共字符串标题{get;set;}
公共虚拟机箱实体机箱{get;set;}
公共虚拟事件实体事件{get;set;}
公共虚拟ICollection设置字段{get;set;}
}
[表(“[BALANCE.SETUP.FIELD]”)
公共类SetupFieldEntity
{
公共Guid SetupId{get;set;}
公共整数序列{get;set;}
公共字符串部分{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共虚拟设置实体设置{get;set;}
}
modelBuilder.Entity()
.HasKey(x=>x.Id)
.HasMany(x=>x.SetupFields)
.WithRequired(x=>x.Setup);
modelBuilder.Entity()
.HasKey(x=>x.SetupId)
.HasKey(x=>x.Sequence);

你的实际问题是什么?嗯,我想这会很清楚。这肯定是我的映射的问题,因为我应该只有一个,而不是两个。这就是问题所在。我需要使其中一个能够读取,另一个能够写入。问题似乎与导航键也是子选项卡中的主键这一事实有关虽然设计是错误的,但我无法将自动编号添加到该表中。返回为空的编号…您是否明确启用延迟加载属性/使用
.Include(表)
?看到了,我知道了,其中一个配置在阅读时可以正常工作,并且集合总是设置好的。似乎您将基于属性的配置与流畅的配置混合在一起。由于您使用
[Key]
属性装饰属性,我认为
.Map(x=>x.MapKey(“SETUPID”))
是冗余的-可能是导致问题的原因?
        modelBuilder.Entity<SetupEntity>()
            .HasMany(x => x.SetupFields)
            .WithRequired()
            .HasForeignKey(x => x.SetupId);
if (includeFields)
{
    x = x.Include(entity => entity.SetupFields);
}
modelBuilder.Entity<SetupEntity>().HasMany(x => x.SetupFields);


[Table("[BALANCE.SETUP]")]
public class SetupEntity
{
    [Key]
    public Guid Id { get; set; }

    public string Title { get; set; }

    public virtual ChassisEntity Chassis { get; set; }

    public virtual EventEntity Event { get; set; }

    public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}

[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
    [Key]
    [Column(Order = 0)]
    public Guid SetupId { get; set; }

    [Key]
    [Column(Order = 1)]
    public int Sequence { get; set; }

    public string Section { get; set; }

    public string Name { get; set; }

    public string Value { get; set; }

    public virtual SetupEntity Setup { get; set; }
}
public class SetupEntity
{
    public Guid Id { get; set; }

    public string Title { get; set; }

    public virtual ChassisEntity Chassis { get; set; }

    public virtual EventEntity Event { get; set; }

    public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}

[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
    public Guid SetupId { get; set; }

    public int Sequence { get; set; }

    public string Section { get; set; }

    public string Name { get; set; }

    public string Value { get; set; }

    public virtual SetupEntity Setup { get; set; }
}

modelBuilder.Entity<SetupEntity>()
    .HasKey(x => x.Id)
    .HasMany(x => x.SetupFields)
    .WithRequired(x => x.Setup);

modelBuilder.Entity<SetupFieldEntity>()
    .HasKey(x => x.SetupId)
    .HasKey(x => x.Sequence);