实体框架不能使用正在创建的DbContext模型 [英] Entity Framework cant use the DbContext, model being created

查看:120
本文介绍了实体框架不能使用正在创建的DbContext模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF 4.1,并创建一个正常的EF edmx文件。
我从一个DB生成它。



当生成它时,我右键单击并选择添加代码生成项,生成新类,然后使用DbContext。我使用模板DbContext生成器。



一切都很好。



然后我试着查询上下文:

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

我创建一个新的上下文,但是当我尝试查询问题发生时。我被困在UnintentionalCodeFirstException上。
并得到错误:


{使用数据库优先和模型开发的T4模板生成的代码可能无法正常工作如果在Code First模式下使用,要继续使用Database First或Model First,请确保在执行应用程序的配置文件中指定了Entity Framework连接字符串。要使用从Database First或Model First生成的这些类,使用Code First使用属性或DbModelBuilder API添加任何其他配置,然后删除引发此异常的代码。}


我不想首先使用代码,但是我不知道我是否可以切换它,或者问题在哪里。



为了参考,这里是我的构造函数...

  public部分类PasDBEntities:DbContext 
{
public PasDBEntities()
:base(PasDBEntities)
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}

...和连接字符串:

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


解决方案

我看到您正在使用EDMX与模板(。 tt)用于生成类。但是,如果您从现有数据库获取信息,向导将创建一个与ObjectContext(元数据信息和实体框架提供程序)兼容的ConnectionString。



问题是您正在使用的connectionstring是用于ObjectContext(数据库优先和模型优先)。对于DbContext,您应该使用没有元数据信息的connectionstring。



您的连接字符串应为:

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


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>

解决方案

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

这篇关于实体框架不能使用正在创建的DbContext模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆