Using VS2017, I have few related tables in DB with constraints etc. I have created blank MVC/WebAPI project with Individual User Account option, installed EF6.2.0, imported entities from DB using 'Code First from Db Model' technique, added keys to Identity User, Role, and Login inside overridden OnModelCreating() and created the first migration. Migration declared all Identity tables (users, roles etc) but also tables which are already in DB which is understandable. When I try Update-Database I get an exception saying that table is already in DB. What is the best practice to solve this problem? I was thinking about commenting out tables in migration leaving only Identity tables.
Related
I am using EF6 with existing db and a separate new schema for my code first.
We were moving from staging to Production, and suddenly realized that all db's code first objects were gone, all beside the migration table.
Of course everybody blamed code first as the cause. Is it possible? or maybe someone deleted the objects manually by mistake?
In my Static Context constructor I used
Database.SetInitializer(new MigrateDatabaseToLatestVersion
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.
I've created an ADO.NET, MVC4, EF5 project which has a relationship between an entity and the built in Membership provider's User.
I have cloned (using git) my repository to an external server and have set up a database with the same name and run the .edmx.sql file created by ADO.NET.
When I run my site on the external server I get the following exception: "Invalid column name 'User_UserId'."
Looking in the database, I can see that Users, Roles, etc. tables have not been created.
What is the best way to do this kind of deployment and rectify my woes?
Update
So I added the tables in question (Users, Roles, UserRole, Applications) to the database by scripting it from the database on my local machine and still the same error occurs.
This seems to be a problem with the mappings in System.Web.Security.Membership.
Any help please?
Okay! Managed to do it. So for anyone else with the same problem - try this:
Create a new DB on the external server
Run the .edmx.sql file on that DB
Change the connection string to your server's name
Run the site locally - this will create the Users, Roles, etc. tables
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
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.