2016-12-27 101 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

我要發佈我的模型生成器。 –