0

I have an error:

An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in DataAccess.dll but was not handled in user code Additional information: 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.

In DataAccess project I have an EF 6 with App.Config file with string:

<connectionStrings>  <add name="CVJobOnlineEntities"  connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model 1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=STEFAN-PC\SQLEXPRESS;initial catalog=CVJobOnline;integrated  security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

and in my second project, which is the main Start-Up project I have in WebConfig:

<connectionStrings>
<add name="CVJobOnlineEntities"
      providerName="System.Data.SqlClient"
      connectionString="Server=.\SQLEXPRESS;Database=CVJobOnline;Integrated Security=True;"/>

So, obviously I am mixing EDMX and CodeFirst conn strings, but, I need it CodeFirst because of my Identity tables which I was incorporate in my SQL SERVER DB.

Also in my DbContext, I recalled base to use FirstCode (Model1.Context.cs):

 public partial class CVJobOnlineEntities : DbContext
{
    public CVJobOnlineEntities()
        : base("name=CVJobOnlineEntities")
    {
    }
5

1 Answer 1

1

You must specify your connection string only once at the entry point of your application. Your DataAccess project does not need a connection string if it is not executable. Cut & paste the connection string from your DataAccess project to the web configuration file of your application entry point, overwriting the old one.

The problem was not exactly mixing two types of connection strings, since the one from DataAccess was never read by the Entity Framework. The one provided at your entry point config was just wrong in your scenario, because you are using model-first and not code-first.

Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation! For now, I let it default conn - for user data, and the other conn string for all other data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.