Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using EntityFramework 6.2, MVC5, and Data first methodology. i.e. I created the database manuall a long time ago.
Was working for many months but earlier today I updated from SQL 2008 developer verson to SQL 2012, yeah I'm a masochist.

I started getting this error recently;
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: 'The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715'

Everything works fine on my development machine but when I publish it I get an 500 server error, I'm assumng because of the meta data in the connection string;
<add name="SQLConnection" connectionString="metadata=res://*/Models.DataModel.csdl|res://*/Models.DataModel.ssdl|res://*/Models.DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=s10.winhost.com;initial catalog=DB_110255_guardian;user id=xxxxxx;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

What I'm wanting to do is return to using ADO.NET connection strings and for to be able to create the edmx file without the system thinking it's a code first DB.
A shove in the right direction would be of great help. The problem is I just don't know what I'm up against.

What I have tried:

I'm at a lose as to what to address here.
I've tried to disable code first - without success
--- Database.SetInitializer<sqlcontext>(null);
--- Deleted the __MigrationHistory table
--- uSoft recommended creating a configuration class, tried it no work!
--- Tried several versions of code in the web.config file in entity framework section with no luck.
--- I've generated the edmx files numerous times
Posted
Comments
Richard Deeming 18-Dec-17 14:32pm    
That exception is usually thrown when your connection string doesn't include the "metadata" stuff. Are you sure you haven't changed the connection string on the server?

With recent versions of EF, you can use Code First with an existing database:
Entity Framework Code First to an Existing Database[^]

You can also migrate from Database First to Code First:
Migrating a Project from Database First to Code First[^]
Mike Hankey 18-Dec-17 14:55pm    
Richard,
Thanks for the response, I tend to get so frustrated and in this case in a hurry that I ask a stupid question out of pure desperation.
After 2 days of research and trial and error I have found that;
- Yes I was always using the meta with my connection string
- I've always had a problem with authorization, turns out I was using the DB on the server but authorization was done by a DB created locally. Maddening to say the least. So I've gone to a custom authorization and role providers and done away with the ASP stuff.
- I did try the Code First approach as many had suggested but to no avail.

Strangest thing is though this morning I told my GF about my problem and she answered back "...you must have fixed your problem because it's up..."
Tried it on FF Developer like I had been doing, no love, went to chrome and IE and it was up. Tried clearing cache, same thing so I don't know what the hell to think about that.

Anyway I'm going to create another web app and transition everything over to it just to be sure there are no remnants taking it a piece at a time.

The hurry up problem I have is that I have 1 week to finish the main part because after that I'm moving to another park (I volunteer at FL state parks) and the park that I'm moving to has very crappy internet.

Anyways thanks for your input, appreciate it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900