The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update - model-view-controller

I am getting error
"The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database"
I could find few answers but i do lose my data.
I am using Identity 2.0.
What is solution to this?

Try to write:
Database.SetInitializer<ApplicationDbContext>(
null
);
into your Application_Start() method in the global.asax file...
(Rename the ApplicationDbContext name to the name you use in your application)

The problem is that you're working with a model that already created the tables, then you changed the model (but no the database), so when it goes and tries to work with the database there is a mismatch between the model and the database.
I don't know if you're working with code first or not, but I suggest you to MigrateDatabaseToLatestVersion initializer which will update the database automatically to match the changes in the model you've done without loosing data.
Hope it helps. Guillermo.

Related

facade.edit does not change the database

I am working on a JSF project that deals with MySQL DB.
in my backbean method I did:
entityFacade.edit(entity object);
after that I wanted to make sure of the changes, I queried my database to retrieve the edited record, However, the retrieved values are the old ones. it is like entityFacade.edit(entity object); was not made.
I solved this problem by doing this
entityFacade.remove(entity object);
entityFacade.create(entity object);
But the problem is that the object has ID which is auto-increment & I want to keep the old ID.
I don't get why the edit does not change values. what are the possible causes of this problem?
Looking forward to your answers, experts.
For those who are facing the same problem, I solved it as follows:
The problem was not with the facade.edit()
it was with the queries.
Try facade.find(id) to retrieve the records from the database instead of queries. The retrieved records will show the edited data.

Symfony 1.4 overloading "default" table method

I have a class called Profile that stores information about the user that is specific to the application. This data probably should have been stored in sf_guard_user_profile, but it wasn't designed that way at the time, and it's too late to modify now.
The problem is that in admin generated modules with filters, Doctrine_Core::getTable("profile") gets called and then a separate query on sf_guard_user_profile is performed for every row in profile.
I am looking for way to override the "default" table method in Profile to add a join to sf_guard_user_profile. I have created custom table methods, and that works fine. However, I would rather find a way to force the join in all cases instead of using a specific table_method. This would save me from having to add the table method to every filter throughout the application.
I thought that I might be able to override getInstance() in the ProfileTable class. However, this has no effect. I've also tried overriding findAll() in ProfileTable, which also does not work.
Is there a "default" table method that is used whenever a table method isn't directly specified?
I hope this makes sense.
Thanks!
-Steve
You should rather try overriding the createQuery() method of the Profile table. This method is called whenever a query is made by the table class so this should do the trick.

Model changed during database created

I have uploaded my MVC3 project , it's s simple blog , at first it works well but after couple hours! following error appears (I've made custom error to Off to see the error)
The model backing the 'SiteContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
to solve this I have to manually delete my database and create again and then restore to the backup that I have created. but after after 2 hours again I get the error!
I really don't have any idea , what caused that ??
When you create a model and ask EF to create a database from it, EF would hash the model and store the hash value with the database. Whenever the context is created, EF recomputes the hash and matches it against what is stored at the database. If the model changes in any way, the resulting hash will be different and EF will throw the exception you have just seen. This is done in order to keep the model in sync with the database.
Is there any way the model could have changed during runtime?
One thing you could do to figure out the difference is to
1.Re-create the database from the model as you are doing now and get it scripted (script1.sql).
2.Wait till the error happens and delete the db and re-create it again and script it (script2.sql)
3.Try to compare the two and see whether you can spot a difference in the schemas.
This should give you an idea of what has changed in the model.
Goodluck

Changed existing entity model and manually updated SQL Server database but still get context changed error

I have an existing MVC3 application and database that is on a SQL Server 2008 R2 and I had to add a bool item to an existing model.
After I added it to the model, I rebuilt and published the project. Then I opened up SQL Server Management Studio and went to the table and added the entry to the column as a bit, I had to make it nullable since the table already contains data.
I thought this would be all that I would need to do to get everything working. However I got this error:
The model backing the 'psllc_DB' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
I'm not sure what else to do, I went back to the code and changed the bool to bool? so it will be nullable but this didn't help either. I can't drop the entire database since it's full of data, however as a last ditch possibility I could drop this table and re-create it cause it only has a few entries, but I don't know if that will work. I'm not sure what else to do so any advice would be very appreciated.
Update
Since I'm not getting any responses, let me rephrase my question.
How should I update my database (a SQL Express mdf file) to add a bool Column to a Table that has data already? I need the database to match my updated MVC 3 Entity Code First model otherwise I get the above error.
Thanks
Since this is code-first, you should do this code-first: change the class and use EF-migrations to change the database. The way you do it, the model and the database may match, but the meta information in the database is not updated.
By the way, if you supply a default value, you can add a non-nullable column.

MVC Linq to SQL Update Object in database

I have a table called Code in my LINQ to SQL datacontext. I also have a class called Codes in my Models folder. What I want to do is save the updated object Codes to my database table Code. Is this possible?
In my controller, I would pass the edited Object to my Model. My CodesRepository file contains this:
public Codes EditCode(Codes CodeToEdit)
{
private EventsDataContext _db = new EventsDataContext();
Codes C = new Codes();
C = CodeToEdit;
_db.Codes.InsertOnSubmit(C); //error here, something about invalid arguments
//InsertOnSubmit is for adding a new object, but I don't know the syntax
// for editing an existing object.
_db.SubmitChanges();
}
This is probably not the correct way of doing this so can someone point me in the right direction? Do I even need a class called Codes or do I need to somehow just use my database table? Thanks.
Solution: I decided to change from Linq to SQL to an Entity Framework and it works much better. This way, I don't have to define my Codes class since it comes straight from the database and I was able to delete the Codes class file.
You should use DataContext.Attach when you get an object back that corresponds to en existing row in the database. For Linq-to-sql's optimistic concurrency handling to work this requires that you either have the original, unsaved object available, or that you have a TimeStamp column in the database. The latter is preferred, as it only requires one extra field to be handled (probably through a hidden field in the web form).

Resources