I have two separate unrelated models that I am trying to use in the same database. Here is the code for each:
public class ImageDBContext : DbContext
{
public ImageDBContext()
: base("DefaultConnection")
{
}
public DbSet<ImageModel> Images { get; set; }
}
[Table("Images")]
public class ImageModel
{
[Key]
public int ID { get; set; }
public string Type { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string FileName { get; set; }
[NotMapped]
public HttpPostedFileBase Attachment { get; set; }
}
Here is the other model
public class TestimonialDBContext : DbContext
{
public TestimonialDBContext()
: base("DefaultConnection")
{
}
public DbSet<Testimonial> Testimonials { get; set; }
}
[Table("Testimonials")]
public class Testimonial
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
When I access the view for one model it works great. It doesn't matter which model but when I go to the view for the other model I get this error:
The model backing the 'ImageDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
It seems to me like both models are trying to use the same table? The table gets created by the first model that is initialized but then the second model tries to use the the existing table instead of creating its own.
How can I get both models to use their own tables in the database?
Here is the connection string:
<add name="DefaultConnection" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=--------;User ID=-------;Password=---------" providerName="System.Data.SqlClient" />
Update: I have added the following code to the models and I still get the same error.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ImageModel>().ToTable("ImagesTable");
}
and
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Testimonial>().ToTable("TestimonialsTable");
}
The error message is deceiving - this cannot/should not be resolved with "Code First Migrations". You have two different DbContexts. If it's the same database then just use one context to which you added both Images and Testimonials.
The way you're doing it, you are basically saying there are two different databases, which use same connection string, and each database has one table, either Images, or Testimonials. So, when you access say Images page, the database gets created with one table Images. But when you access Testimonials it recognizes that the DB structure does not match the model of one Testimonials table and warns you.
Create one DB context like so:
public class MyDBContext : DbContext
{
public DbSet<ImageModel> Images { get; set; }
public DbSet<Testimonial> Testimonials { get; set; }
}
And in your controllers just create a new instance of this class when you need to access DB. If you name your connection string (in web.config) as "MyDbContext" you can skip the empty constructor where you need to call base("DefaultConnection").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With