EF Core 如何添加主键

Jua*_*mez 6 c# entity-framework

我在 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); }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是当我尝试运行Add-Migration 时,抛出

System.InvalidOperationException: The entity type 'Ejercicios' requires a primary key to be defined.
Run Code Online (Sandbox Code Playgroud)

如何将主键添加到 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

Val*_*rie 7

你为什么不使用fluent api

modelBuilder.Entity<Ejercicios>()
    .HasKey(p => new p.TipoEjercicio);
Run Code Online (Sandbox Code Playgroud)

试试这个,我想你的问题现在已经解决了。

- -更新 - -

创建您的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());
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的应用程序根目录中创建一个迁移文件夹并在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" }
        //    );
        //
    }
}
Run Code Online (Sandbox Code Playgroud)

我是一个 Germ Freak,所以我写的代码非常干净。这就是为什么当例如我在Model下面创建一个像时,我EntityBase为每个创建一个Id

public class EntityBase
{
    public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并将其实施到我的Model

public class User: EntityBase
{
    public string Example1{ get; set; }
    public string Example2{ get; set; }
    public string Example3{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

对于映射,我创建了另一个如下所示的类并使用Fluent Api

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        ToTable("TblUser");
        HasKey(x => x.Id);
        Property(x => x.Example1)
            .IsRequired();
        //etc

    }
}
Run Code Online (Sandbox Code Playgroud)

但是如果你不想经历所有的麻烦,你可以很容易地将流畅的 api 插入你的DbContext's OnModelCreating方法中,就像我在开始时说的那样。顺便说一下,如果您使用的是 fluent api,则不应该使用数据注释。快乐编码。