简体   繁体   English

实体框架 6:上下文在 Code First 模式下与从 EDMX 文件生成的代码一起使用

[英]Entity Framework 6: The context is being used in Code First mode with code that was generated from an EDMX file

I'm creating WPF application using Entity Framework 6 with database first approach.我正在使用实体框架 6 和数据库优先方法创建 WPF 应用程序。 EF did everything for me and i'm able to connect to the database and so far downloading data works, so connection exists. EF为我做了一切,我能够连接到数据库,到目前为止下载数据工作,所以连接存在。 But every time i add user control it gives me new error that connection string was not detected.但是每次我添加用户控件时,它都会给我新的错误,即未检测到连接字符串。 Program works fine anyway, but its hard to program when your error window is flooded with it.程序无论如何都可以正常工作,但是当您的错误 window 被它淹没时,它很难编程。

I have found out that changing context from:我发现从以下方面改变上下文:

public MediaRentalEntities()
     : base("name=MediaRentalEntities")
{
}

To this:对此:

public MediaRentalEntities()
     : base("MediaRentalEntities")
{
}

Should fix the issue, but it doesn't.应该解决这个问题,但它没有。 Now it throws exception (and shows this error multiple times, based how many user controls i have, currently 3):现在它抛出异常(并多次显示此错误,基于我有多少用户控件,目前为 3):

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.上下文在 Code First 模式下使用,代码从 EDMX 文件生成,用于 Database First 或 Model First 开发。 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.如果您希望使用 Database First 或 Model First,请确保 Entity Framework 连接字符串包含在启动项目的 app.config 或 web.config 中。 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.如果要创建自己的 DbConnection,请确保它是 EntityConnection 而不是其他类型的 DbConnection,并将其传递给采用 DbConnection 的基本 DbContext 构造函数之一。 To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715要了解有关 Code First、Database First 和 Model 的更多信息,请首先参阅此处的实体框架文档: http://go.microsoft.com/fwlink/?LinkId=394715

This is my current app.config:这是我当前的 app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
  </startup>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="MediaRentalEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;data source=localhost;initial catalog=MediaRental;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

It's starting to be tiring, i'm tempted to change to EntityFrameworkCore or even Dapper, it feels like EntityFramework6 is against the user, but WPF has way more functionality on .NET Framework.开始累了,我很想换成 EntityFrameworkCore 甚至是 Dapper,感觉 EntityFramework6 对用户不利,但 WPF 在 .NET 框架上有更多功能。

in connection string replace MyModel with " MediaRentalEntities ".It will work在连接字符串中将 MyModel替换为“ MediaRentalEntities ”。它将起作用

Or或者

If you have generated it using DB first then you will already have context class where code is like如果您首先使用 DB 生成它,那么您将已经有了上下文 class 代码就像

 public MyModel()
     : base("name=MyModel")
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架:上下文在代码优先模式下使用,代码是从 EDMX 文件生成的 - Entity Framework: The context is being used in Code First mode with code that was generated from an EDMX file 上下文正在Code First模式中使用,其代码是从EDMX文件生成的 - The context is being used in Code First mode with code that was generated from an EDMX file 在Code First模式中使用上下文,其中包含从EDMX文件生成的用于Database First或Model First开发的代码 - 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 实体框架EDMX首先编码 - Entity Framework EDMX to Code First 实体框架4.1代码优先EDMX问题 - Entity Framework 4.1 Code First EDMX issue 实体框架代码首先在Controller中使用上下文 - Entity Framework Code First using context in Controller 生成的实体框架代码 - Generated Entity Framework Code 在Entity Framework Power工具beta 2中首次使用反向工程师代码后,移动生成的类 - Move generated classes after Reverse engineer code first used in Entity Framework Power tools beta 2 实体框架4,将一个edmx文件的上下文扩展到另一个edmx文件的上下文 - Entity Framework 4 , Extending context of one edmx file to a context of another edmx file 将基础实体上下文用于代码优先实体框架 - Using Base Entity Context for code first Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM