ASP.NET MVC Code First Data migrations with renamed database tables - asp.net-mvc

I am developing a ASP.NET MVC web application using code first with entity framework.
The problem I am facing is that I've made a rename of a couple of tables using data migrations and apparently everything went fine I even see the table names updated in the database.
But when I create a new data migration it always takes the original name of the table (xxxxes), not the latest (xxxxs) and I have to change manually the migration script to avoid the error:
Cannot find the object "dbo.xxxxs" because it does not exist or you do not have permissions.
And what is worse, the index controller now fails because when it tries the ToList() method it throws the following error:
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: An error occurred while executing the command definition. See the inner exception for details.
Being inner details:
{"Invalid object name 'dbo.xxxxs'."}
I've looked in the whole project to try to find the place where this tables are still referenced with the original name but I've found nothing.
Any idea about how to solve this??
EDIT
What I did, and maybe here is where everything broke is create a data migration like this and run it.
public partial class Update5 : DbMigration
{
public override void Up()
{
RenameTable("xxxxes", "xxxxs");
}
public override void Down()
{
}
}
I've seen that the Down() method should have just the opposite action, which I did not add...

You can try to add the table names via data-annotations.
Check this link
By default, EF will use the name of your class to name the table. If you changed the table name, EF will not know which class corresponds with which table. So if you add the name of the table with an annotation above the class, EF will know what to find.

Related

codefirst, renaming table, trigger error

I have my project that is a Xamarin.Forms app, with a Azure SQL Database and an Azure Mobile App Service.
I am using code-first to create the database.
Now I want to rename a class "Customer" to "Company".
The migration is done correctly, the existing table is renamed.
However, there is a trigger, created automatically, that is renamed as well, but the table name that is uses is not modified. So the triggers doesn't work anymore.
I suppose the trigger exists because of the "ServiceTableColumn" convention added to the modelbuilder.
This is the generated migration code:
public partial class rename_customer_to_company_step_1 : DbMigration
{
public override void Up()
{
RenameTable(name: "dbo.Customers", newName: "Companies");
}
public override void Down()
{
RenameTable(name: "dbo.Companies", newName: "Customers");
}
}
This is the script for the trigger (after executing the above migration):
CREATE TRIGGER [dbo].[TR_dbo_Customers_InsertUpdateDelete]
ON [dbo].[Companies]
AFTER INSERT, UPDATE, DELETE AS
BEGIN
UPDATE [dbo].[Customers]
SET [dbo].[Customers].[UpdatedAt] = CONVERT(DATETIMEOFFSET, SYSUTCDATETIME()) FROM INSERTED
WHERE inserted.[Id] = [dbo].[Customers].[Id]
END
As you can see, the table itself has been renamed (ON [dbo].[Companies]
), but the contents of the triggers still references the previous table name (UPDATE [dbo].[Customers]).
Of course the trigger does not work.
How can I have the trigger modified correctly? Is the trigger really necessary? Can I get rid of these triggers?
Any ideas how to correct this? I prefer not to update the triggers manually, why am I using code-first migrations if I need to do that?
Thanks in advance,
Joris
If the code-first migration didn't do it, you will have to edit the code-first migration file or update the triggers yourself. The triggers are relatively easily done if this is a one-time thing. See chapter 3 of the Zumo Book at http://aka.ms/zumobook for details on this.

MVC EF (Code First) - Creating relationship with AspNetUsers and Custom Tables

new to MVC and EF. Trying to achieve the following:
A new site with individual user accounts
Create a DB table "Leagues" whereby when creating a new league the LeagueAdmin is an ApplicationUser
I followed this tutorial to try to achieve what i wanted. When i start the application I am able to register new users without issue. However, when I go to create a new league I 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 have tried many fixes i've read about but they tend to lead me down a path of increasingly complex errors. I feel like this should be simple enough that i'd like to understand this error first before tackling the next.
VS Project Source is here
Most probably, your database does not contain a _MigrationHistory table.
Add this code to your DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
Go to Package Manager Console by following View -> Other Windows -> Package Manager Console
And write following commands:
enable-migrations
add-migration InitialCreate
update-database
Alternatively;
You can add this code to your DbContext;
static LeagueContext()
{
Database.SetInitializer<LeagueContext>(
new DropCreateDatabaseAlways<LeagueContext>());
}

EF4 database first configurable schema

I have one issue I am trying to resolve for days now, but I can’t get the right approach.
I am using EF4 and I have one application where I use DataBase First, which originally created the ObjectContext, and I donwloaded the DbContext generator and generated it.
The thing is, I need the application to be able to get the database SCHEMA from some configuration file, instead of ALWAYS using the “dbo” default.
I was trying to use the “ToTable” method (so I can specify the schema) in the “OnModelCreating” overload method but as this article sais, as I am using DataBase First, that method is not called.
How can I make the schema name configurable?
Is that even possible?
I read this article too, where it says I can combine database first with code first but I can’t see how to do that if I can’t use the "OnModelCreating" method.
Thanks a lot in advance!!!
I don't know about configuring schema. However if you want your db first approach to changed to the code first, just change the string parameter of your DbContext constructor.
Suppose that you have the following DbContext that EF Db first created for you:
public class MyDbContext : DbContext
{
public MyDbContext()
: base("Name=DefaultConnection")
{
}
// DbSets ...
}
change that to the following to start using code first and all magic tools of it (migration, etc.):
public class MyDbContext : DbContext
{
public MyDbContext()
: base("YourDbFileName")
{
}
// DbSets ...
}
It causes that EF creates a new connection string using SQL Express on your local machine in your web.config file with the name YourDbFileName, something just like the early DefaultConnection Db first created.
All you may need to continue your way, is that edit the YourDbFileName ConStr according to your server and other options.

Entity Framework 5 delete my database changing model

I have an ASP.NET MVC 4 project with Entity Framework 5, .NET 4.5 and Visual Studio 2012.
In my solution I've put all the models in a project called Model, all the Repositories and my DbContext in one more project called Data.
I activate the migrations in the Data project with the Enable-Migrations command. I decide to handle them manually. If I create a new migration with the Add-Migration command everything works very well. If, for example, I add a new column to a table, it works fine. I can see the new column in the database schema and I see the new record into the _MigrationHistory table.
At this point, with the new column created, I need to add this column to the right model. So, i add this method to my code-first model class and I run the project.
It delete my database, and init it with the initial migration.
I can't tweak a model without loosing all data.
How I can avoid this behavior?
Thanks
UPDATE:
Configuration.cs
namespace NegoziazioneEventi.Data.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<NegoziazioneEventi.Data.NeDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(NegoziazioneEventi.Data.NeDataContext context)
{
}
}
}
Application_Start() in Global.asax
protected void Application_Start()
{
// init basic data on database
System.Data.Entity.Database.SetInitializer(new Models.InitData());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
_container = Bootstrapper.GetWindsorContainer();
}
I decide to handle them manually. If I create a new migration with the Add-Migration command everything works very well.
...
At this point, with the new column created, I need to add this column to the right model. So, i add this method to my code-first model class and I run the project.
That is completely wrong usage of migrations and it is also the reason why EF deletes your database. You must first add property to model and then add migration because EF needs to store correct data into _MigrationHistory table to match that record with the real meaning of that migration.
With your current approach EF runs the application and checks _MigrationHistory table but the record in the table doesn't contain information about your newly added property so EF believes that new change was done to your model and uses default strategy to delete database and create a new one reflecting your current model. You can turn off this behavior but you should start by using migrations correctly!
To turn off the behavior use:
Database.SetInitializer<YourDatabaseContext>(null);
You are using your own initializer which is most probably derived from wrong build-in initializer causing drop of your current database.

Asp mvc 3 noobie: Why is the code-first method not building my DB on sql server?

I am an ASP MVC 3 noobie who has done a few tutorials. Now I'm trying to build a site. All of the tutorials on the microsoft website emphasize the code-first approach: you define your model with code and then create a datacontext and then the entity framework creates/manages the DB based on your code.
I set up an Employees class and a DataBaseContext class that inherits from DbContext. I added a connection string to Web.config connection string that successfully links DataBaseContext to an already existing empty DB on SQL server. EDIT= That was the problem. See my answer below
But when I try to run the Employees controller created thru scaffolding, I get this error
Invalid object name 'dbo.Employees'.
Description: An unhandled exception occurred during the execution of...
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Employees'.
I followed this post SqlException (0x80131904): Invalid object name 'dbo.Categories' and realized that if I create an employees table on the DB, this excpetion goes away (I get a new one saying that the column names are invalid).
But I thought the whole point of MVC 3 is that the framework will make the DB for you based on the code.
Maybe I need a line of code in the Global.asax Application_start() to create the database? Here is my application_start method:
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
RegisterGlobalFilters(GlobalFilters.Filters)
RegisterRoutes(RouteTable.Routes)
End Sub
Here is the code for Employee:
Public Class Employee
Property EmployeeID As Integer
Property First As String
Property Last As String
Property StartDate As DateTime
Property VacationHours As Integer
Property DateOfBirth As DateTime 'in case two employees have the same name
End Class
Here is the code for the DB context:
Imports System.Data.Entity
Public Class DatabaseContext
Inherits DbContext
Public Property Employee As DbSet(Of Employee)
Public Property AnnualLeave As DbSet(Of AnnualLeave)
End Class
What am I missing?
By default EF uses DropCreateDatabaseIfModelChanges<TContext> database initializer. Accordingly to the MSDN:
An implementation of IDatabaseInitializer<TContext> that will delete, recreate, and optionally re-seed the database with data only if the model has changed since the database was created. This is achieved by writing a hash of the store model to the database when it is created and then comparing that hash with one generated from the current model.
Since the database was created manually, EF can't find the hash and decides do not perform any further initialization logic.
You might want to look into this article, same question successfully answered already.
Or it can be this (also resolved successfully)
Answer to your problem is most likely one of the two.
Hope this will help you
Does the name you're specifying for your connection string match the name of your database context?
For example:
Context
var myDbContext = new MyDbContext();
Connection string
<connectionStrings>
<add name="MyDbContext" connectionString="YOUR.CONNECTION.STRING" providerName="System.Data.SqlServer" />
</connectionStrings>
Try and see if this post I wrote about DbContext with MVC works for you: Code-First
Not a lot to be done to get this to work, but there are a few things that are easily missed that will cause a bunch of head aches.
hope this helps
I had already created a database with that name on SQL server. Once I deleted the existing database, the code first framework created the tables for me like it was supposed to. It seems like if the database already exists, the framework won't set up the tables for you. It wants to create the whole DB from scratch.
You were using AdventureWorks Database?
It has it's own schema assigned to the employees table. HumanResources.Employees and not the default dbo.Employees.
Even though I've identified the problem, I don't know the solution to using the database as configured with the HumanResources schema.
Anybody know?

Resources