Ich habe eine neue DbContext mein Projekt wie folgt aus:Erste Daten aus einer neuen DbContext mit Linq
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("myapp_users");
modelBuilder.Entity<IdentityRole>().ToTable("myapp_roles");
modelBuilder.Entity<IdentityUserRole>().ToTable("myapp_userroles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("myapp_userclaims");
modelBuilder.Entity<IdentityUserLogin>().ToTable("myapp_userlogins");
}
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyDbContext : DbContext
{
public MyDbContext()
: base("TestConnection")
{
}
public static MyDbContext Create()
{
return new MyDbContext();
}
}
Aber wenn ich versuche, einige Daten mit diesem Befehl zu erhalten:
MyDbContext myContext = new MyDbContext();
var products = (from p in myContext.Products
select p).ToList();
Ich habe Dieser Fehler:
'MyDbContext' does not contain a definition for 'Products' and no extension method 'Products' accepting a first argument of type 'MyDbContext' could be found (are you missing a using directive or an assembly reference?)
Irgendwelche Hilfe zu diesem Thema? Wie kann ich die Daten aus einer Tabelle in der Datenbank eines neuen Kontexts abrufen?
Wo ist das 'IDbSet Produkte'? –
@HristoYankov Ich habe es nicht. Soll ich für jede Tabelle, die ich in der DB habe, ein neues Modell anlegen? –
Ich nehme an, Sie gehen "Code-zuerst", richtig? Dann ja, Sie brauchen eine Klasse 'Product' und Sie müssen hinzufügen' public IDbSet Produkte {get; einstellen; } 'in Ihrer Db-Kontextklasse. –