COMException when trying to update EDMX from DB after separating POCO classes - c#

When Test.edmx, Test.context.tt, & Test.tt were all together, I was able to update edmx from db with no issues.
After separating Test.tt class from EDMX & moving it to a new project where all the POCO classes reside, I started seeing the below exception when trying to update edmx.
An exception of type 'System.Runtime.InteropServices.COMException' occurred while attempting to update from the database. The exception message is: 'A file or folder with the name 'Test.Context.tt' already exists. Please give a unique name to the item you are adding, or delete the existing item first.'.
Environment
Visual Studio 2013 with Update 5
Entity Framework 6.1.3
.NET Framework 4.5.1
Console Application & a Class Library.

I had the same issue, and I freaked out looking for a solution, but at the end found out that ignoring the exception is of no harm. It's just trying to rewrite the tt file, and it's not a necessarily step in updating your model really.
The edmx gets updated as expected, and you can run the custom tool for your tt files that are scattered everywhere, and they will work nicely.
Just make sure that all the tt files are linked to the edmx file.
const string inputFile = #"..\YourProject\Model.edmx";

Related

How to safely drop Compact SQL database migration history

I'm developing a WPF app utilizing a SQL Server Compact 4.0 database. For the course of this project, we made several changes in the model structure with a number of code-based and later with automatic migrations. They all are stored in the _MigrationHistory table and represented by several migration classes.
Now it appeared that I need to considerably change the model. Since the app is still in the development phase, dropping data in the database is not a concern. I would like to get rid of the whole previous migration history and start a new clean model. So my question is what steps are to remove the whole history of migrations and start a new history with automatic migrations in the future.
What parts of the project must be deleted and how to do it safely?
First Make sure your project is backed up and your database is also backed up. In case something goes wrong.
If your project is setup using entity framework code first,
In the project you can delete all the migration files in the 'Migrations' folder except 'MigrationConfiguration.cs' contained within the project that is using entity framework.
Also if you are not concerned with dropping the database, then go-ahead and delete the entire database, then create a new one with the same name that is configured in your database context connection string found in your project's config file.
Rebuild your solution and create a new migration by typing the command in the package manager console 'add-migration' and provide a name. This will create a new migration to create the database for all your code first models. If the generated migration file looks correct, then run 'update-database' in the package manager to apply the migration to your empty database.

Copying project does not retain Entity Framework connection

I'm moving my project from one machine to another. I simply zipped the project folder and moved it to my new computer. My project is an asp.net mvc application with Entity Framework. I have my connection string in my web.config file. When I run my application on my new machine, it has trouble connecting to the database it seems.
I get this error when trying to create a dbcontext
[SqlException (0x80131904): There is already an object named 'Merchandises' in the database.]
But the odd thing is it runs perfectly fine on my old machine.
I executed add-migration command and it says I'm very behind in migrations but this is not true.
Unable to generate an explicit migration because the following explicit migrations are pending: [201802070210548_Initial_Migration, 201802130149472_Added_TrackForm, 201802181945057_Added_DOB]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
Any assistance for debugging this would be greatly appreciated.
You can remove all migrations and add only one that named Initial_Migration ( add-migration 'Initial_Migration' and update-database).
--- reference from github.

MVC project with connection string not working for other colleagues in TFS

We are working on an MVC application, I added the connection string and generated the data models. When I update the model from database in the .edmx file it updates the web.config file and I check it in TFS. When other other people get latest version from TFS, they see the connection but when they go to the .edmx file it has update the model from database grayed out? is there any way to fix this?
First, make sure your colleagues are opening the project which in source control in VS, not the local one.
Another thing is going into the workspace and change the local path not containing '#' character. More detail info about this, please take a look at this thread: Entity Framework Unable to refresh database tables under TFS 2010

The model backing the '--Context' context has changed since the database was created - but db is new production database

I've got this error for the 762nd time but this time I am getting it as soon as I attempt to access my Production site, straight after deleting the 'production' database on Azure and then publishing my site.
The model backing the 'PropertyContext' context has changed since the database was created. Consider using Code First Migrations to update the database
I deleted the database because I couldn't fix this issue any other way but it still doesn't work.
Some important points:
I'm using EF6 and publishing to Azure.
This is 1 of 2 projects/sites that uses the same Repo project. I have no
problems with the other one, just this one.
I have tried publishing the problem project first (after deleting the db) and
second with the same result.
I have tried deleting both WEBSITES and the DB from Azure and starting again
I have tried deleting all migrations and starting with a fresh data model
I have tried the following in my Global.asax (in both projects)
Database.SetInitializer PropertyContext>(null); <-- SO won't let me put the first <
and
Database.SetInitializer(new MigrateDatabaseToLatestVersion<PropertyContext, MyConfiguration>());
new PropertyContext().Database.Initialize(true);
I'm using .net 4.5
Why am I getting this error on a new database and how can I get this site to work?
Just ran into the same error in ASP.Net application. In my case I did not use Code First, but I used standard ASP.Net authentication provider which apparently uses Code First, and authentication was broken because of this issue.
Here is quick and dirty solution is you don't care much about existing user records:
For me the solution was to drop the dbo.__MigrationHistory table, authentication started working fine after that. Be aware! This solution is not for everyone! This will fix the problem, but it is potentially risky.
If you cannot afford to lose data in AspNet* tables:
ASP.Net authentication provider automatically creates tables in your database:
AspNetRoles
AspNetUsers
AspNetUserRoles
AspNetUserClaims
AspNetUserLogings
The tables are empty by default, if you haven't created any new logins for your web site, you can use "quick and dirty" solution above. If you do care about preserving user information or just curios how Code First migrations work, follow these steps:
Open your Web.config file and check the name of the connection string you have for your database. It will be one of the records under <connectionStrings> element.
Open Package Manager Console:
Tools –> Library Package Manager –> Package Manager Console
In Package Manager Console window, use a drop-down to set Default Project. Make sure this is the project that contains ASP.Net authentication provider code.
Execute command:
Update-Database -ConnectionStringName MyConnectionStringName
Replace the MyConnectionStringName with the actual name you looked up in web.config.
As a result of this command you will see a new folder "Migrations" with a bunch of code generated by the Update-Database command. Re-build and re-deploy your app, your new migration code will be executed on startup and would bring the database schema in sync with an updated version of ASP.Net authentication provider code.
When using Code First with Migrations, your database creates a table called __MigrationHistory to track the current schema. When you run your application your Entity Framework will check this table to make sure that the database schema matches your database entities. If they do not match, you will get this error.
To update your database follow these steps:
Open the Package Manager Console (View -> Other Windows -> Package Manager Console) in Visual Studio
In the Package Manager Console Window, there is a drop down with your projects in, make sure it is set to the project that contains your DbContext
Make sure that the project that contains your App.Config / Web.Config file is "Set as Startup Project" (If you have multiple Configs, it must be the one with the Database Connection String defined.
Type Update-Database -ConnectionStringName MyConnString where MyConnString is the name (not the actual connection string) of your connection string in your App.Config / Web.Config
If you get an error like this: "Unable to update database to match the current model because there are pending changes and automatic migration is disabled."
You should enable Automatic Migrations and try again.
To enable Automatic Migrations
In the Migrations folder (in the project with your DbContext), open Configuration.cs.
Make sure the Constructor contains: AutomaticMigrationsEnabled = true;
To stop Entity Framework/DbContext from monitoring changes on your database you could simply delete the __MigrationHistory table in your database. It is then up to you to make sure that the database remains updated manually.
MSDN article here
The solution from this is to use the static method SetInitializer and bind to the context a Null value. If you are working on a Web solution, the best position to write the code is in the Application_Start of your Global.asax.cs file.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
//...
Database.SetInitializer<MyContext>(null);
}
I got a similar problem this morning. Suddenly the error appeared and couldn't be resolved:
The model backing the 'ApplicationDbContext' context has changed since
the database was created. Consider using Code First Migrations to update
the database
I have one project for MVC and another project for the model, context and repositories. I've been working on it for weeks but today it said stop.
I have tried to delete database, enable-migration, add-migration and update-database so many times that I've lost count. I've added initializers to MigrateDatabaseToLatestVersion as well as DropCreateDatabaseIfModelChanges. All in vain...
What finally made it work was to move model, context and repositories into the MVC project (not something I was keen on)...then it worked right out of the box without any code changes at all (besides namespaces)! Very strange...
I've read so many blog posts during the day trying to solve this problem. One of them (I don't know which one) mentioned a bug in Visual Studio 2013 where reference to DLL files weren't always updated as they should, suggesting that my MVC project missed out something when I was running add-migration and update-database in my separate project. But it's just a guess.
I'm using EF 6.1 and .Net 4.5.1 in my solution.
Got a similar problem! Answer is here
http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc3/cs/adding-a-new-field
(Rick Anderson)
There are two approaches to resolving the error:
Have the Entity Framework automatically drop and re-create the database based on the new model class schema. This approach is very convenient when doing active development on a test database, because it allows you to quickly evolve the model and database schema together. The downside, though, is that you lose existing data in the database — so you don't want to use this approach on a production database!
Explicitly modify the schema of the existing database so that it matches the model classes. The advantage of this approach is that you keep your data. You can make this change either manually or by creating a database change script.
I have spent some hours trying to solve this problem. One project was working, the other one wasn't.
I had different projects referencing different versions of Entity Framework. In my case, I had a Console app and a Windows Service app, both referencing a third project containing code first classes and DbContext.
After running Update-Package EntityFramework everything worked fine in both projects.

Entity Framework model update on a remote machine

I'm building a web application that uses the Entity Framework. The files are located on a remote machine, the same is for the database and the web server.
In visual studio (2k8sp1), the path to the project is: \\Server\Web\XXXX
Now, I've generated the EF entities from the database, and later I've updated the database and added there a stored procedure.
When I wanted to update my model after that in visual studio, I was getting an error message that says:
An exception of type Systen.ArgumentException occured while attepting to update from the database. The exception message is: 'A relative path is not possible for files on different devices'.
Any ideas on how to update my model?
ps. I also cannot change my connection string in the EF model designer.
I think you might be using the Entity Framework incorrectly. The edmx files are source code, and should be treated as such. They should be added to your project, checked into source control, and edited locally on the development machine.
Try importing your model from your database into a local edmx and see if that works better for you.
You can also use EDMTools2 tool for update your EDM Schema like that. Also you can use this tools routines in your code it's open source. Just copy inside of your project and call some functions like create model, create code from model..
I hope this will be help to you

Categories

Resources