[英]EF Core how add Primary Key
我在 SQLite 的 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); }
}
}
現在我的問題是當我嘗試運行Add-Migration 時,拋出
System.InvalidOperationException: The entity type 'Ejercicios' requires a primary key to be defined.
如何將主鍵添加到 sqlite 的 EF Core 模型?
編輯 1:模型生成器
public class MyContext : DbContext
{
public DbSet<Ejercicios> Ejercicios { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=MyDb.db");
}
}
你為什么不使用fluent api ?
modelBuilder.Entity<Ejercicios>()
.HasKey(p => new p.TipoEjercicio);
試試這個,我想你的問題現在已經解決了。
- -更新 - -
首先創建您的DbContext
:
public class MyDbContext : DbContext
{
public MyDbContext()
: base("name=MyConnection")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, YourApplication.Migrations.Configuration>("MyConnection"));
}
public DbSet<Users> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//here you can MAP Your Models/Entities, but i am going to show you something more interesting. so keep up.
modelBuilder.Configurations.Add(new UsersMap());
}
}
在您的應用程序根目錄中創建一個遷移文件夾並在那里創建Configuration
類:
internal sealed class Configuration : DbMigrationsConfiguration<YourApplication.Infrastructure.Data.MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "YourApplication.Infrastructure.Data.MyDbContext";
}
protected override void Seed(YourApplication.Infrastructure.Data.MyDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
我是一個 Germ Freak,所以我寫的代碼非常干凈。 這就是為什么當例如我制作一個像下面這樣的Model
,我為每個Id
創建一個EntityBase
:
public class EntityBase
{
public int Id { get; set; }
}
並將其實施到我的Model
:
public class User: EntityBase
{
public string Example1{ get; set; }
public string Example2{ get; set; }
public string Example3{ get; set; }
}
對於映射,我創建了另一個如下所示的類並使用Fluent Api
:
public class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
ToTable("TblUser");
HasKey(x => x.Id);
Property(x => x.Example1)
.IsRequired();
//etc
}
}
但是,如果您不想經歷所有麻煩,您可以輕松地將流暢的 api 插入DbContext's OnModelCreating
方法中,就像我在開始時所說的那樣。 順便說一下,如果您使用的是 fluent api,則不應該使用數據注釋。 快樂編碼。
如果您使用 EF 核心添加
.UseSerialColumn();
例子
modelBuilder.Entity<JobItem>(entity =>
{
entity.ToTable("jobs");
entity.Property(e => e.Id)
.HasColumnName("id")
.UseSerialColumn();
});
我使用 EF.postgres
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.