0
我有這個類定義到SQLite的Ef核心模型。EF核心如何添加主鍵
public class Ejercicios : BaseModel
{
private int _TipoEjercicio;
[Key]
public int TipoEjercicio
{
get { return _TipoEjercicio; }
set { SetProperty(ref _TipoEjercicio, value); }
}
private string _DescripcionEjercicio;
public string DescripcionEjercicio
{
get { return _DescripcionEjercicio; }
set { SetProperty(ref _DescripcionEjercicio, value); }
}
private string _HexForeColor;
public string HexForeColor
{
get { return _HexForeColor; }
set { SetProperty(ref _HexForeColor, value); }
}
private string _HexBackGroundColor;
public string HexBackGroundColor
{
get { return _HexBackGroundColor; }
set { SetProperty(ref _HexBackGroundColor, value); }
}
}
現在我的問題是,當我嘗試運行附加遷移,拋出
System.InvalidOperationException: The entity type 'Ejercicios' requires a primary key to be defined.
如何添加主鍵的EF核心模型的源碼?
編輯1:模型生成
public class MyContext : DbContext
{
public DbSet<Ejercicios> Ejercicios { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=MyDb.db");
}
}
必須添加此代碼的位置?我在模型生成器嘗試,但無法找到名爲** OnModelCreating。**的方法來覆蓋它 –
@JuanPabloGomez您使用EF Code-First嗎? – Valkyrie
我要發佈我的模型生成器。 –