asp.net mvc model database change - asp.net-mvc

I've used mvcScaffolding and mvc3 to generate my tables in a sql2008 database. some data has been added.
I've changed a single model and wish to alter the underlying table.
is there a way to auto-update a single table to keep it in sync with the model?
after adding a new model how can the database table be created without recreating the database

You're looking for something like Rails' migrations. There are a couple .NET migrations providers floating around, but the basic setup you've described doesn't support alterations to the schema without recreating the database (at least in my experience).

have you tried treating your database an an existing one as described in this example by Scott Gu?
http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
Hopefully thias way you will not have to re-create your db everytime a change is required...
Regards
Paul

Related

Creating model from ado.net code

Is there a way to create a model from an ado.net SqlDataReader? I would really prefer to use Asp.net MVC 5 coming from Ruby on Rails rather than Webforms, but I need a way to display the data from an existing database on the view. Or could I possibly do this without creating a model by handling this in the controller? I don't want to create a model based on column names in the table in case the table (or db schema) changes later on.
Use Entity Framework`s DbContext.
Looking around, the normal solution would be to reverse-engineer the model from the database found here: http://www.codeproject.com/Articles/671590/Reverse-Engineering-an-Existing-Database-in-your-A
Unfortunately, this is something that usually has to be done through Visual Studio, which I will not always have access to once the application is in production mode. If the schema changes, I would not be able to use the Update Model from Database... command in VS 2013. Therefore, I'll have to use Asp.net Web Forms for my Database-First application.

Model compatibility error

Setup
I have an app that uses ASP.NET Identity 2.0. The identity part shares a database with the rest of the tables needed by the application. So in one class library, I have a dbcontext that accesses the database for business data, and in another class library, I have the IdentityModel.cs, ie, the ApplicationDBContext.
Problem:
All worked fine, until I got in a muddle, trying to figure out how to work with migrations with the business data context. I ended up deleting the __MigrationHistory table and hence all the model metadata in the database for both the context.
I now get the following 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.
I deleted all migrations related to the business data, and re enabled migrations for that project. That had the effect of recreating the model metadata for the business dbcontext.
However, I can't figure out how to achieve the same for the Identity metadata.
Question:
How do I recreate the model metadata in __MigrationHistory for ASP.NET Identity 2.0?
Write below code in Global.asax.cs and try again...
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TsContext>());
I had to recreate the database and start again. I am still in the dark to the extent that I don't know if Neel's answer is the correct one for a production database. Right now, I don't have the time to properly research AspNet.Identity to find the solution to the problem. Sorry.
Prevention before Cure:
What I DO know is that the problem would not happen if I hadn't deleted the __MigrationHistory table. So the issue is one of self harming and so prevention rather than curing is the best option.
Therefore, the moral of the story is:
Moral:
Never, EVER, delete the __MigrationHistory table. Learn a bit more about it BEFORE doing any thing quite so silly, feckless, carless and downright dumb.

Creating asp.net membership tables

I am using Entity Framework code first, so keep dropping and re-creating the database. My asp.net membership tables keep disappearing as well - particularly annoying on the build server.
The best way I have found to create the tables so far involves a post build event running aspnet_sql.exe, but the connection strings need to be hard-coded and its making staging-release environments difficult.
Is there an elegant way to create the tables in code?
Have a look at Universal Providers. Then you don't need to use aspnet_sql.exe.
Also use migrations rather than drop and re-create database.
Why are you dropping and re-creating tables so often? Maybe migrations could solve this issue for you.

EF4 and ASP.Net MVC to Test and Develop without mapping to an underlying database (until later)

I want to develop an ASP.Net MVC application with EF4 Model First design and only generate the actual database much later, ideally at the end of the project.
Based on some of the ideas here:
http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/
I want to create something like an InMemoryObjectContext for testing and development and use IOC to switch to the SQL Server implamentation (EF generated) for UAT and production.
Is this wise?
Is it possible? If so, does anyone have any suggestions?
Does EF always need an underlying database in order to track changes, commit etc?
I've tried creating a model first but as soon as I add properties I get the following errors:
Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer Model1Container.
and the warning:
Running transformation: Please overwrite the replacement token '$edmxInputFile$' with the actual name of the .edmx file you would like to generate from.
The error doesn't stop the application running but worries me. I'm very very new to EF so I apologize if this is way off the mark or a dumb question. I'm hoping to get some good advice while I sit for the next few days and watch videos and read articles.
Thanks
Davy
At the very least you need mapping information "filled in". You can fill these fields with nonsense if you don't want to work against the underlying database.
If your doing Model first, right click on the designer canvas and select, "Generate Database from Model". This will automatically create convention based mappings for you without defining tables and columns. You don't even need a valid db connection to do this.

Creating Models in ASP.NET MVC

I'm just starting a project in ASP.Net MVC with LINQ to Entities and I was wondering if there was a nice, clean way of defining models that creates the appropriate tables in the database for me. I'm most familiar with Django (in terms of MVC frameworks) and am looking for the .Net equivalent of models.py so I can have everything versioned. Any ideas? It would be even better if it had some form of schema migration, a la django-evolution and the like.
I think what you want to do is to turn the question around. Entities can be automatically generated from the database, so the issue is simply using a .NET mechanism to maintain your database schema. Since you're not using NHibernate, which these other solutions require, I would suggest using MigratorDotNet. MigratorDotNet uses exactly the same idea as Ruby on Rails migrations:
Your database keeps track of its version
Every time you wish to change the schema, you write a small class to handle the upgrade (and, optionally, downgrade)
Assign these classes an execution order
If the database is ever not up-to-date, simply execute the classes' upgrade methods in order
Since you'll only be regenerating your Entities at compile time, I'd recommend running the migration scripts, and then regenerating your entities, as a part of your build process. MigratorDotNet already comes with an MSBuildTarget, adding it will just involve a couple of clicks.
Another option is to use NHibernate with FluentNhibernate which will auto map your model based on conventions. You can also override a mapping to tweak it for your needs.
Castle Project active record is a nice way of doing it.
If offers capabilities similar to ruby on rails active record.

Resources