Entity Framework cant use the DbContext, model being created

江枫思渺然 提交于 2019-12-17 20:57:30

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/7944596/entity-framework-cant-use-the-dbcontext-model-being-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!