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
Related
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
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";
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.
Always in my projects i add a ADO.net Data Model (from database connection) to my Data Access Layer Project that i named it DataProject, but now i need to use SQL Server Databse project that i named it SQLDatabaseProject.
Now, i want how can i make relation between My DataProject and SQLDatabaseProject?
I mean as my old way adding "ADO.net Data Model" from database connection to DataProject and separately i can deploy db or change tables in SQLDatabaseProject!! i want to know is there anyway to make model in DataProject from SQLDatabaseProject? not from my old way?
for more details i used Visual Studio 2012 and i used C# with .net 4.5 and my projects in my solution is like this:
TestSolution
SQLDatabaseProject
DataProject
and i added reference from SQLDatabaseProject in DataProject but i cant see any namespace of data or something else when i want to create model or ...!!?
There are two ways you can do this:
1) You can use T4 templates to generate code for you from the scripts you have in the database project.
2) Have a model and dev database on your machine and then simply do the db changes on model and update those changes on your database project and (Eg. Entity Framework). When you're happy with the changes made to the model database (which should be empty), then publish the changes to the dev database.
I have a very simple web part. I have a single grid view, which I am populating using linq to entities (or at least that's what I want to do). The Entity Data Model .edmx file is located in the same project as the web part, and everything looks to be in working order. When I debug the project, it blows up on the entity model constructor with the error message:
The specified named connection is
either not found in the configuration,
not intended to be used with the
EntityClient provider, or not valid.
My connection string in the App.Config is as follows:
<add name="MyDBEntities" connectionString="metadata=res://*/MyDBEntityModel.csdl|res://*/MyDBEntityModel.ssdl|res://*/MyDBEntityModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
The constructor:
public MyDBEntities() : base("name=MyDBEntities", "MyDBEntities")
So, from what I've read elsewhere, my problem is that SharePoint can't see my connection string. Which means, that the App.Config from my project isn't actually getting loaded into SharePoint when I run/debug the project. If that's the case, then how I do set my project up in Visual Studio 2010 to ensure SharePoint picks up the App.Config in addition to the master SharePoint config file. If I have to manually copy the connection string, is there a "best practice" procedure for doing so? Are SharePoint Web Parts combined with the Entity Framework just not ready for prime time?
The SharePoint Tools for Visual Studio 2010 have come along way and will automatically make many of the necessary entries into web.config. Unfortunately, they won't make Entity Framework entries for you. To do this, you'll need to write a feature receiver for your web part project that adds the EF connection string.
The SharePoint API has an object named SPWebConfigModification. You should write a FeatureActivated event that uses this class to make your modification to web.config and then a FeatureDeactivating event that removes the modification.
-Greg
I was wrestling with a the same exception in a SharePoint 2010 WebPart, and I've finally got it working, but here are two important things that I learned along the way.
You must use a farm solution rather than a sandbox solution. The reason for this is, sandbox solutions do not have access to data outside the site collection. A more meaningful exception would have been helpful to solve this quickly, but I was receiving the exception as above.
Your connection string must be in the web.config of the Web Application that you install your WebPart on. It is not added automatically when you install your WebPart, so you must either update the web.config the way Greg lists above, or edit it manually. It sits in C:\inetpub\wwwroot\wss\VirtualDirectories{WebApplicationName}\web.config