Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework cant use the DbContext, model being created

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>
like image 294
Fore Avatar asked Oct 30 '11 11:10

Fore


1 Answers

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" />

like image 189
jvitor83 Avatar answered Sep 30 '22 17:09

jvitor83