I have a class which is used in Entity Framowork and in my VMs;
The class:
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;
}
}
Implementation of 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();
}
}
Using in my VM (StructureSource i usi in ComboBox):
StructureSource = new ObservableCollection<LegPartDbStructure>(base.Data.BPVHips.LevelStructures(number).ToList());
Everything was fine until i needed to add one more field to LegPartDbStructure. The idea is to store a name from metrics instead of having only ids.
[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;
}
But i can't do this because of 'Unknown column 'Extent1.Metrics' in 'field list'' - my table in db doesn't have this field. Does it mean I need to add extra class which has the same info, but stores metrics? How to add this logic properly?
SOLUTION:
I lust needed to add [Not Mapped] to my new field:
[NotMapped]
public string Metrics { get; internal set; }