1

我有一个在 Entity Framowork 和我的虚拟机中使用的类;

班上:

public abstract class LegPartDbStructure
{
    [Key]
    [Column("id")]
    public int Id { get; set; }

    [Column("название1")]
    public string Text1 { get; set; }
    [Column("название2")]
    public string Text2 { get; set; }

    [Column("id_метрики")]
    public int? Size { get; set; }
    [Required]
    [Column("уровень_вложенности")]
    public int Level { get; set; }

    public override string ToString()
    {
        return Text1 + " " + Text2;
    }
}

IRep的实现:

[Table("БПВ_на_бедре_структура")]
public partial class BPVHipStructure :LegPartDbStructure, ILegPart
{

}

public class MySqlContext : DbContext
{
    public DbSet<BPVHipStructure> BPVHipStructures { get; set; }
    public DbSet<BPVHipCombo> BPVHipCombos { get; set; }
    public DbSet<Metrics> Metrics { get; set; }
    public MySqlContext() : base("server=localhost;user=root;database=med_db;password=22222;") {

    }
}

public class BPVHipRepository : Repository<BPVHipStructure>
{
    public BPVHipRepository(DbContext context) : base(context)
    {
    }

    public IEnumerable<BPVHipStructure> LevelStructures(int level)
    {
        return dbContext.Set<BPVHipStructure>().Where(bpvhip => bpvhip.Level == level).ToList();
    }
}

在我的 VM 中使用(我使用 ComboBox 中的结构源):

StructureSource = new ObservableCollection<LegPartDbStructure>(base.Data.BPVHips.LevelStructures(number).ToList());

一切都很好,直到我需要向 LegPartDbStructure 添加一个字段。这个想法是从指标中存储一个名称,而不是只有 id。

[Column("id_метрики")]
public int? Size { get; set; }
[Required]
[Column("уровень_вложенности")]
public int Level { get; set; }

public string Metrics { get; set; }

public override string ToString()
{
    return Text1 + " " + Metrics + " " + Text2;
}

但我不能这样做,因为'字段列表'中的'未知列'Extent1.Metrics' - 我在db中的表没有这个字段。这是否意味着我需要添加具有相同信息但存储指标的额外类?如何正确添加此逻辑?


解决方案

我渴望将 [未映射] 添加到我的新字段:

[NotMapped]
public string Metrics { get; internal set; }
4

0 回答 0