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