Reputation: 6027
I am trying to connect entity to an existing database, but am having a hard time figuring out why I am getting the following error:
The model backing the 'RPSContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
As I mentioned, I am connecting to an existing database. So I don't need entity to modify structure. I'd just like to be able to select/insert/update/etc.
public class ProductAttributePriceAdjustment
{
[Key]
public int AdjustmentId { get; set; }
public int StoreProductId { get; set; }
public int StoreId { get; set; }
public string ProductId { get; set; }
public int ProductSizeId { get; set; }
public decimal Adjustment { get; set; }
public int PointsAdjustment { get; set; }
public int ProductColorID { get; set; }
}
public class RPSContext : DbContext
{
public RPSContext() : base("ApplicationConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ProductAttributePriceAdjustment>().ToTable("ProductAttributePriceAdjustment");
}
public DbSet<ProductAttributePriceAdjustment> PriceAdjustments { get; set; }
}
Upvotes: 3
Views: 21512
Reputation: 2372
In your dbcontext file , add :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<MYDBCONTEXT>(null);
base.OnModelCreating(modelBuilder);
}
Upvotes: 0
Reputation: 11
Go Package Manager Console then go and check your migration history from migration folder and check for first time enable migration name
then
add-migration [FIRST TIME ENABLE MIGRATION NAME] -force
then update-migration
Problem Solved
Upvotes: 1
Reputation: 17579
Almost every time I see this error it's because the database schema and the schema the code is expecting is not synced up. At some point the schema changed and you did not reflect your changes in code.
For example, you have a db column Adjustment
, which is a money type that allows null values. However, in your c# class, it is of type decimal
, which does not accept null values. There are also some other properties that are either not present, or the types don't match up.
Good news is that fixing it should be really easy if you are using database-first. It's as simple as going to your .edmx file, right-clicking an empty area, and choosing "Update Model from Database".
From there you can add, update, or delete items found in your database. Once you save the model file VS will recreate your model classes, and it should be synced up.
Just a note: if you rename a db column, VS will not delete the old column name from your model, and will throw errors until you delete the column from your code model manually.
Upvotes: 2