2016-12-27 100 views
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"); 
    } 
} 

回答

2

你为什么不使用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" } 
     // ); 
     // 
    } 
} 

我是一个胚芽怪,所以我写我的代码很干净。这就是为什么当例如我犯了一个Model像下面,我创建一个EntityBase为每Id

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 

    } 
} 

但是,如果你不想去通过所有的麻烦,你可以只需在你的DbContext's OnModelCreating方法中插入流利的api就像我在开始时所说的那样。顺便提一下,如果你使用流利的api,你不应该使用数据注解。快乐编码。

+0

必须添加此代码的位置?我在模型生成器尝试,但无法找到名为** OnModelCreating。**的方法来覆盖它 –

+0

@JuanPabloGomez您使用EF Code-First吗? – Valkyrie

+0

我要发布我的模型生成器。 –