Entity Framework "Code First" not creating database tables - asp.net

I've created my objects in ASP.NET MVC4, and after running the application, only UserProfile table is auto created, not the other one corresponding my class.
Followed this advice: Entity Framework 4.1 Code First not creating tables and got this error:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
Question 1: What is the correct way to do this in Code First approach?
Question 2: Why is the built-in UserProfile created, and not the table for my object? What makes the difference?

Please read my answer here ... same situation I think. UserProfile table created, others not created. My guess is that you've switched to an actual MSSQL DB and not a local DB, but have not pointed your WebSecurity DB initialization to the new database.

Related

Customizing Identity table with database first

Trying to build an application that uses ASP.NET Authentication with Identity.
I've been watching a video on how to do things. They add new properties directly to the ApplicationUser class and then update the database using code-first migrations.
But I'm using database first.
So I added my new properties directly to the ApplicationUser class, and I also added compatible fields to my database. But I still get an error.
The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
I have Googled this and found articles specifically referencing database first, but they all seem to involve using code first!
Is this even possible to do without code first any more? Or is it just the case where we will all use code first or die?
As #Kyle suggested, this is all based on the __MigrationHistory table.
I'm not sure exactly how this all works, or what the overhead is if this table is being tested against my data each time I run my software. But renaming the table gets rid of the error. And I'm now able to modify the database as I wish without errors.

How do i update an existing database to support ASP.NET Identity 2.0

I have a web solution that I upgraded to ASP.NET Identity 2.0. The problem i now face it that i have some solutions of this software running in production at several customers. When I try installing the new software (with ASP.NET Identity 2.0) on the existing database (full of their data). I then get an error: "The model backing the "ApplicationDbContext" context has changed singe the database was created"
How do i update the database in the existing solution to a database that fits the new ASP.NET Identity? I looked at the ASPnetUsers table and there has been added several new columns.
Is there a way to disable this check? Or is there a way to fix it?
Personally, I would create a new project/database containing just the ASP.Net Identity 2.0 features and create a dummy user (just to make sure that it all works correctly).
Then, use schema comparison between the new database and your existing database. You can then generate scripts to create the user tables in your customers' database.
You may still have further integration work to do, but it should get you over the hump.
I suppose you don't use EF code first. You could run your project in dev using code first and get the full DB structure including the Identity tables, then compare against the prod schema and implement the differences.
This should give you the hint:
http://blogs.msdn.com/b/webdev/archive/2013/12/20/updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1.aspx

Validate EF Code first model against existing database

Is there a way to check that a DbContext matches the database when the database was not created by EF code first?
I am looking for similar functionality to Database.CompatibleWithModel but there is not metadata.
There is currently no way in EF to do this; however, you may be able to use the DDL script as a starting point for verifying that all the artifacts exist in the database. To get this script, use
string ddlScript = ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabaseScript();
Some tools may be able to use this script to do a schema compare against your database. This will tell you if your model is compatible.
Have you tried using Entity Framework Power Tools.
you can use the tools to Reverse Engineer Code First - Generates POCO
classes, derived DbContext and Code First mapping for an existing
database.
And then maybe you can compare the reversed engineered information with what you already have.

Stored procedures get unmapped on data model changes in Entity Framework 4

I'm using VS2010, .NET 4.0, and Entity Framework 4 on a new project. I've been building out the data model iteratively and just started adding some stored procedures. Every time I "Update Model From Database," the stored procedure mappings disappear from my project and it breaks at runtime. I assume I'm missing something fairly simple. Has anyone seen this happen? Do I have something checked or unchecked that's torching my proc mappings with each update?
Thanks!
Are your project have setup the permission to see the stored procedures and function import mapping.
There is existing thread which might help you
vs-2010-edmx-stored-procedure-missing
Remove the procedure from your Data Model and again add it from DataBase.

EF 4.1 Code First and Existing Database and .NET Membership

I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box.
I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message:
The server encountered an error
processing the request. The exception
message is "Model compatibility cannot
be checked because the database does
not contain model metadata. Ensure
that IncludeMetadataConvention has
been added to the DbModelBuilder
conventions.
After some googling, I discovered that I had to delete the database and let EF create the database. That's fine but I lost all my .NET membership tables. I can go back in and add the membership tables again but if my model changes and EF needs to recreate the database then I have to add the membership tables in again.
How do I get around this?
This is how code-first work. Main idea of code first is that you do not touch your database because it is responsibility of the model to create the database. If you want to customize your database you must create custom IDatabaseInitializer and add your custom SQL.
public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
protected override void Seed(MyContext context)
{
// Here run your custom SQL commands
context.Database.ExecuteSqlCommand("CREATE TABLE ....");
}
}
Now you only need setup your cutom intializer on the startup of your application:
Database.SetInitializer<MyContext>(new MyDbInitializer());
If you don't want to do it this way you must manually maintain your database and set initializer to null.
Found a easier workaround here. I hope this helps.
http://www.paragm.com/ef-v4-1-code-first-and-asp-net-membership-service/
Another option could be to use the System.Web.Management namespace. I've had great success with the code below:
string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseContext"].ConnectionString;
string database = "MyDatabaseName";
SqlServices.Install(database, SqlFeatures.All, connectionString);
It will just create the database and after that you can add users with the standard membership API.
Here's another possibility.
If you look at the MvcMusicStore sample - there's a SampleData class that is responsible for seeding the database on a rebuild. The SampleData class inherits from DropCreateDatabaseIfModelChanges, and overrides the Seed method. This class is passed to the Database.SetInitializer in the Application_Start method in global.asax.
I was getting the same error as you until I changed the parent class of SampleData to CreateDatabaseIfNotExist.
Then you can override the Seed method to insert any data you desire at startup, without it blowing away the database.
While you are developing, create 2 databases and two connection strings. One for SqlMembership (using aspnet_regsql) and one for your EF Application. If you would like to merge them into a single DB in production, just change the connection string in web.config.release to be the same. Then, EF model changes will just drop your apps db and not your membership DB.
By treating your authentication component separately, you will naturally decouple your authentication system from your application system. Then, if you wish to change membership providers, you will be better setup.
As the system grows, you will likely need to support non-pure models without EF code first, so this is a good template for going down that path.
I found the easiest way without playing with anything else was the following.
I ran the application first time with DropAndRecreatedatabase always in the Initilizer.
This created my database for the first time.
Following this I changed this to DropCreateDatabaseIfModelChanges.

Resources