Reputation: 373
I added a new DbContext to my project like this:
[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();
}
}
But when I try to get some data with this command:
MyDbContext myContext = new MyDbContext();
var products = (from p in myContext.Products
select p).ToList();
I have this error:
'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?)
Any help on this? How can I get the data from a table in the database of a new context?
Upvotes: 0
Views: 4378
Reputation: 4120
If you go code-first, you need a model for every entity, which will map (generate) to a database table. It usually means that the database will be generated based on your code, although there is the scenario where you have an existing database and you still go code-first.
i.e. you would need:
public class Product
{
// ...
}
public class MyDbContext : DbContext
{
// ...
public IDbSet<Product> Products { get; set; }
}
If you do have an existing database, it might be easier for you to add an EDMX
to your project and generate the context from it. Simply, you 'integrate' with an existing database instead of generating one.
Upvotes: 2