代码优先连接字符串
Code First connection string
我有一个错误:
An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in DataAccess.dll but was not handled in user code
Additional information: 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. 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. 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.
在 DataAccess 项目中,我有一个带有 App.Config 字符串的 EF 6 文件:
<connectionStrings> <add name="CVJobOnlineEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model 1.msl;provider=System.Data.SqlClient;provider connection string="data source=STEFAN-PC\SQLEXPRESS;initial catalog=CVJobOnline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
在我的第二个项目中,这是我在 WebConfig 中的主要启动项目:
<connectionStrings>
<add name="CVJobOnlineEntities"
providerName="System.Data.SqlClient"
connectionString="Server=.\SQLEXPRESS;Database=CVJobOnline;Integrated Security=True;"/>
所以,显然我混合了 EDMX 和 CodeFirst conn 字符串,但是,我需要它 CodeFirst 因为我的身份表已合并到我的 SQL 服务器数据库中。
同样在我的 DbContext 中,我回忆起使用 FirstCode (Model1.Context.cs) 的基础:
public partial class CVJobOnlineEntities : DbContext
{
public CVJobOnlineEntities()
: base("name=CVJobOnlineEntities")
{
}
您只能在应用程序的入口点指定一次连接字符串。如果您的 DataAccess
项目不可执行,则不需要连接字符串。将 DataAccess
项目中的连接字符串剪切并粘贴到应用程序入口点的 Web 配置文件中,覆盖旧的。
问题不完全是混合两种类型的连接字符串,因为来自DataAccess
的连接字符串从未被Entity Framework读取过。在您的入口点配置中提供的那个在您的场景中是错误的,因为您使用的是模型优先而不是代码优先。
我有一个错误:
An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in DataAccess.dll but was not handled in user code Additional information: 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. 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. 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.
在 DataAccess 项目中,我有一个带有 App.Config 字符串的 EF 6 文件:
<connectionStrings> <add name="CVJobOnlineEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model 1.msl;provider=System.Data.SqlClient;provider connection string="data source=STEFAN-PC\SQLEXPRESS;initial catalog=CVJobOnline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
在我的第二个项目中,这是我在 WebConfig 中的主要启动项目:
<connectionStrings>
<add name="CVJobOnlineEntities"
providerName="System.Data.SqlClient"
connectionString="Server=.\SQLEXPRESS;Database=CVJobOnline;Integrated Security=True;"/>
所以,显然我混合了 EDMX 和 CodeFirst conn 字符串,但是,我需要它 CodeFirst 因为我的身份表已合并到我的 SQL 服务器数据库中。
同样在我的 DbContext 中,我回忆起使用 FirstCode (Model1.Context.cs) 的基础:
public partial class CVJobOnlineEntities : DbContext
{
public CVJobOnlineEntities()
: base("name=CVJobOnlineEntities")
{
}
您只能在应用程序的入口点指定一次连接字符串。如果您的 DataAccess
项目不可执行,则不需要连接字符串。将 DataAccess
项目中的连接字符串剪切并粘贴到应用程序入口点的 Web 配置文件中,覆盖旧的。
问题不完全是混合两种类型的连接字符串,因为来自DataAccess
的连接字符串从未被Entity Framework读取过。在您的入口点配置中提供的那个在您的场景中是错误的,因为您使用的是模型优先而不是代码优先。