I am developing in MVC 4, EF 5.0
Model First development - how do I 'easily' get the DB schema to update, without having to drop the database / erase any data?
e.g. I have a Customer table. In this table, we capture name, surname, email address. I now want to collect the telephone number, so I change the model and get EF to update the database schema.
Visual studio 2012 referrs me to http://go.microsoft.com/fwlink/?LinkId=238269, however this looks like chinese to me.
They talk about running an NUGET package called code first migrations. Does this functionality not come packaged automatically with VS2012?
Thanks
Database migrations is what you are looking for. Both automatic and specified in code -
http://msdn.microsoft.com/pl-PL/data/jj591621
Related
I'm trying to overcome the following issue...
I have a MariaDB database that is used for an IOS application.
I'm about to release a new version to the apple store that uses a different version of the schema of the database. means all achievement related tables are modified.
can liquibase be used to configure the changes and allow selected users to connect and work with the schema as if the old schema version is configured, and to actually make the actual changes to the new schema?
before I release the application I need to provide apple with a test version so they will confirm it. so I want users that are connected from apple to see the new achievements features and to work with the new schema while regular users to still be able to user to previous version of the application and to work with the old schema that behind the scenes will actually update the database according to the new schema.
I hope I explained myself properly.
can Liquibase do that ? or it's just like a git for db changes ?
thank you! :)
I created the databases first, then used the wizards in Entity Framework 5 to create the models from the Database first. Over time, through development, I made changes to the models and let EF recreate them by deleting my database and then starting the project up and they magically appeared again with the changes I made to the columns . It worked great on SQL Express 2014. But Godaddy doesnt let EF do that, AND I have databases with names like "Hazards.Models.CompanyDataContext" that EF made and didnt ask me what I wanted to name them. How do I change the code so it will let me rename the database it uses, and if possible, incorporate it into the aspnetdb that mvc uses as an additional table (there already is simplemembership etc. in there.)?
Seeing as you used EF database first, rather than deleting your database and letting EF recreate it, have you thought about doing it the other way round?
You could use something like management studio express to rename your database tables/columns however you see fit. Then in Visual Studio, open your .edmx file, right-click the background and select 'update model from database'. This way you can add new tables to the model or update the models of existing tables from the database. Better still, you can take care of it all outside of godaddy.
Note: I found that when updating the EF model, just updating existing tables gives odd results sometimes. It's much more reliable when you delete tables from your .edmx first, then right-click and select 'update model from database', and add the tables back to your model.
How to re-create the database using EF6?
I have tried both of following post but, i don't know why its not working and getting same error.
How do I generate an EF6 database with migrations enabled, without using update-database?
Migrations is enabled for context ''but the database does not exist or contains no mapped tables
I have already published my sample on the web server. I am using Sql Server 2012 Express DBMS.
When i have created my application and published it on web server it was working fine. It has created database automatically. After that i have made some changes in ApplicationDBContext and added some properties in IdentityModes(ApplicationUser). Then I have migrated database on my Local IIS Express and it was working fine. Then I have tried to publish my sample again on web server but, this time it shows an error.
Migrations is enabled for context 'ApplicationDbContext' but the database does
not exist or contains no mapped tables. Use Migrations to create the database
and its tables, for example by running the 'Update-Database' command from the
Package Manager Console.
I have deleted the database on web server and tried again. but, still i am facing same error. Now, there is an empty database without table.
Application is creating database on User Registration.
I have also read this post and tried to call dbContext.Database.Initialize(true); and ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabase() method but, still its not working.
First, automatic migrations are nice for development but it's a hugely bad idea to rely on them in production. Second, at some point you turned off automatic migrations, which is why your production database is no longer having tables created in it.
Go ahead and leave automatic migrations off; even in development it's better to know exactly what changes you're making to your database by generating a migration, seeing the code that will be executed, and only then running Update-Database to apply those changes.
You have a number of choices in terms of getting schema updates into production, but they all boil down to roughly the same procedure.
Generate a new migration against your local database.
If you want to update your production database via SQL, run Update-Database -Script. This will generate a SQL file with the code to run on the database to migrate the schema. Hand this off to your DBA if you have one or review the SQL code yourself and then apply it manually to your database via Management Studio.
Run Update-Database. This time without -Script to make the changes to your local database schema.
If you didn't generate the SQL file in step 2, view your local database in SQL Server Object Explorer in Visual Studio. Right-click on the local database there and choose, "Schema Compare...". Follow the wizard to compare with your production database. In the end, you can either generate a SQL file with the necessary changes that need to be made (which again, you'd hand off to your DBA if you have one), or you can migrate to production directly from Visual Studio. However, this is not really recommended. It's always best to generate the SQL, which you or your DBA can then inspect before applying the changes live.
Chris Pratt is correct BTW.
I had downloaded a project in which I needed to just run "Update-Database
Here are the quick screenshots
Then at the bottom of Visual Studio
PM> Update-Database
DONE
I mean I was using a .SDF file but then had issues getting that to work with my host. So then I went back to the SQL DB I already have but I noticed that when I go to the default MVC 4 index page it is looking for that entity that I already created when I used the original DB.. but this table does not exist with the new SQL DB I switched to..
I already verified that is is pointing to the new DB buy setting the connection string in the constructer.
How do I tell the code to 'RE CREATE THE TABLE' before trying to use it ?
If I understand your question correctly, you're looking for something like Entity Framework Migrations: http://www.hanselman.com/blog/EntityFrameworkCodeFirstMigrationsAlphaNuGetPackageOfTheWeek10.aspx
I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0 and Entity Framework 4.1 Code First. It works fine locally, but in production, it fails with the following error:
The model backing the 'foobar' 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've verified that my application dll and database file are up-to-date on the remote server, but I continue to get this error.
Any idea what the problem could be?
Note: I'm using the RTF version of the Entity Framework 4.1, not CTP.
Edit
Okay, if I delete the EdmMetadata table, and re-publish the database to the production server, I no longer get the error. However, I completely don't understand why I should have to do this. The model has not changed. It hasn't changed in weeks, and I rebuilt the database just today. I guess the key to this whole thing is the hash. What is the EdmMetadata hash being compared against?
Edit 2
According to Mark S's answer, the hash in the EdmMetadata table is compared against a hash of the SSDL. However, there is no actual SSDL file (no file with extension .ssdl and no file containing "ssdl") anywhere in the solution. So, where is the SSDL actually stored? Is it generated at design time or run time?
Edit 3
To answer my own question, SSDL does get generated and cached at runtime (more details here), but looking at the actual schema of SSDL, I'm not sure I see anything that could possibly change simply because the project's DLL lives in a different location on a different server.
Note: I'm using the exact same database file (.sdf) for test and production. I'm literally ftp-ing the file from bin/App_Data to the App_Data folder on the production server. I'm also ftp-ing the project DLL. So, the only thing that should be different between test and production is that these files live in a different place.
Also, just for the record, here is my connection string:
DataSource=|DataDirectory|foobar.sdf" providerName="System.Data.SqlServerCe.4.0
Edit 4 (final edit?)
The problem turned out to be a DLL issue. I think a different version of the EntityFramework DLL was being used remotely vs. locally. The solution was to make sure I had the latest version of EF 4.1 (RTW), clear and re-add references, regenerate the database, and re-publish. Everything is now working great.
In response to "What is the EdmMetadata hash being compared against?"
The EdmMetadata table allows Code First to determine if the model in your code is the same as the model in your database. The table contains a single row which is a hash of your SSDL. SSDL stands for Store Schema Definition Language and is an XML based language used to describe an Entity model and the mappings between models.
You can read more about SSDL and view the specification at http://msdn.microsoft.com/en-us/library/bb399559.aspx
You mention that things work fine locally but not in production. Are you allowing Code First to build both the local and production databases? Is the production database already existing or being generated in another fashion?
CodeFirst is reporting that the hash in the database doesn't match the present model's hash so something must be different. Figuring out what may be a challenge.
The EdmMetadata table is mapped to the EdmMetadata class. There is a TrygetModelHash method in this class which you can use to get the hash for a given context. If you're desparate you could use this to compare your current model to the hash that's in your production database. This would at least keep you from having to build a new database constantly just for testing.
You can also instruct Code First to exclude the EdmMetadata table if desired.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
Disable validation:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
Check out this video:
http://www.pluralsight-training.net/microsoft/players/PSODPlayer.aspx?author=scott-allen&name=mvc3-building-data-i&mode=live&clip=0&course=aspdotnet-mvc3-intro
This is part of the free plural sight training available on http://asp.net/mvc. Click the "When Classes Change" video on the left side of the page, then go to 8:30 in the video.
Apparently if you delete the EDMMetadata table in the DB, EF will disable model validation.