Entity Framework cant use the DbContext, model being created

14,462

I see you are using the EDMX with Templates (.tt) for generating the classes. But if you are getting the information from a existing database, the wizard will create a ConnectionString compatible with ObjectContext (metadata informations and provider of entityframework).

The problem is that the connectionstring you are using is for ObjectContext (Database First and Model First). For the DbContext you should use the connectionstring without the metadata informations.

Your connection string should be:

<connectionStrings>
<add name="PasDBEntities" 
     connectionString="data source=localhost;
                       initial catalog=PasDB;
                       integrated security=True;
                       pooling=False;
                       multipleactiveresultsets=True;
                       App=EntityFramework"
     providerName="System.Data.SqlClient" />

Share:
14,462
Fore
Author by

Fore

Updated on July 21, 2022

Comments

  • Fore
    Fore almost 2 years

    I am using EF 4.1, and I create a normal EF edmx file. I generate it from a DB.

    When it's been generated I rightclick and select add code generation item, to generate new classes, and use the DbContext instead. I use the template DbContext generator.

    Everything works fine.

    Then I trie to query the context:

    using (var context = new PasDBEntities())
    {
        var client=context.ClientCompanies.SingleOrDefault(_=>_.ID==clientCompanyId);
        if(client!=null)
    

    I have no problem creating a new instance of the context but when I try to query it the problem occur. I get stuck on the UnintentionalCodeFirstException. And gets the error:

    {"Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception."}

    I don't want to use code first, but I don't know if I can "switch" it off, or where the problem is.

    For reference, here is my constructor ...

    public partial class PasDBEntities : DbContext
    {
        public PasDBEntities()
            : base("PasDBEntities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    

    ...and connection string:

    <connectionStrings>
        <add name="PasDBEntities" 
             connectionString="metadata=res://*/PasDB.csdl|
                                        res://*/PasDB.ssdl|
                                        res://*/PasDB.msl;
                               provider=System.Data.SqlClient;
                               provider connection string=&quot;
                               data source=localhost;
                               initial catalog=PasDB;
                               integrated security=True;
                               pooling=False;
                               multipleactiveresultsets=True;
                               App=EntityFramework&quot;"
             providerName="System.Data.EntityClient" />
    </connectionStrings>
    
    • Slauma
      Slauma over 12 years
      Is this connection string in the "config file of executing application" (to quote the exception) and not only in a config file of a library project?
    • Fore
      Fore over 12 years
      You are so right. I didn't know that I had to add an extra config file for the connectionstring in my unit test project to be able to use the EF. That solved my problem, adding another app.config file.
  • Pawel
    Pawel over 11 years
    actually in EF5 you should be able to use edmx and entity connection with DbContext. In VS2012 T4 were changed to generate DbContext for DatabaseFirst.
  • Worthy7
    Worthy7 almost 8 years
    I was actually trying to move from Code first, into Database first. Your answer indicated that we need to use the special metadata filled string - thanks for that. Basically what this means is when using the ADO.NET wizard, the "Save connection string" NEEDS to be used.