2016-12-27 9 views
0

Ich habe diese Klassendefinition in Ef-Core-Modell für SQLite.EF Core, wie Primary Key hinzufügen

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); } 
    } 
} 

Nun mein Problem ist, wenn ich versuche Add-Migration, wirft

für SQLite
System.InvalidOperationException: The entity type 'Ejercicios' requires a primary key to be defined. 

Wie fügt Primärschlüssel zu einem EF Core-Modell zu laufen?

Edit 1: Modell Generator

public class MyContext : DbContext 
{ 
    public DbSet<Ejercicios> Ejercicios { get; set; } 


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     optionsBuilder.UseSqlite("Filename=MyDb.db"); 
    } 
} 

Antwort

2

Warum nicht fluent api verwenden?

modelBuilder.Entity<Ejercicios>() 
    .HasKey(p => new p.TipoEjercicio); 

Probieren Sie dies aus, ich denke, Ihr Problem ist jetzt gelöst.

--- --- Aktualisierung

Designs DbContext Erstens:

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()); 
    } 
} 

eine Migration Ordner in Ihrer Anwendung Wurzel erstellen Und es Configuration Klasse machen:

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" } 
     // ); 
     // 
    } 
} 

Ich bin ein Germ Freak, also schreibe ich meine Codes sehr sauber. Deshalb, wenn ich zum Beispiel ein Model wie unten gemacht, erstelle ich ein EntityBase für jeden Id:

public class EntityBase 
{ 
    public int Id { get; set; } 
} 

und setzt es auf meinen Model:

public class User: EntityBase 
{ 
    public string Example1{ get; set; } 
    public string Example2{ get; set; } 
    public string Example3{ get; set; } 
} 

Und für Mapping ich eine andere Klasse erstellen, wie unten und verwenden Fluent Api:

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

    } 
} 

Aber wenn Sie nicht möchten, dass durch all die Mühe gehen, können Sie Setzen Sie einfach die flüssige API in Ihre DbContext's OnModelCreating Methode ein Wie ich am Anfang sagte. Beachten Sie übrigens, wenn Sie fließende API verwenden, sollten Sie keine Datenanmerkungen verwenden. Glückliche Kodierung.

+0

Wo muss dieser Code hinzugefügt werden? Ich habe am Modellgenerator versucht, konnte aber keine Methode namens ** OnModelCreating. ** finden, um sie zu überschreiben. –

+0

@JuanPabloGomez verwenden Sie EF Code-First? – Valkyrie

+0

Ich werde meinen Model Generator posten. –