jjc*_*c99 240 c# entity-framework asp.net-mvc-4
我正在使用实体框架和ASP.NET MVC 4来构建应用程序
我的解决方案分为两个项目;
我的问题是,当我尝试使用' MyEntites'DbContext时,我收到以下错误:
在应用程序配置文件中找不到名为"MyEntities"的连接字符串.
我想这个问题与连接字符串位于类库的app.config而不是MVC项目中有关.
有没有人有什么建议?
Jer*_*rry 303
尝试将连接字符串复制到MVC项目中的.config文件.
Ore*_*ren 141
你是对的,这是因为类库(.edmx文件所在的位置)不是你的启动/主项目.
您需要将连接字符串复制到主项目配置文件.
如果您的启动/主项目没有配置文件(就像在我的控制台应用程序案例中那样)只需添加一个(启动项目 - 添加新项目 - >应用程序配置文件).
可以在此处找到更多相关信息: MetadataException:无法加载指定的元数据资源
CMS*_*CMS 93
确保你的项目(使用DbContext)作为启动
要么
添加到app.config(或web.config)中设置为启动连接字符串的项目
要么
像这样调用命令
Update-Database -Script -ProjectName '<project name>' -StartupProjectName '<project name>' -ConnectionString 'data source=.;initial catalog=<db name>;integrated security=True;MultipleActiveResultSets=True' -ConnectionProviderName 'System.Data.SqlClient'
然后再试一次
Ser*_*gan 29
你可以把连接字符串传递给EntityFramework你的生活:
public partial class UtilityContext : DbContext
{
static UtilityContext()
{
Database.SetInitializer<UtilityContext>(null);
}
public UtilityContext()
: base("Data Source=SERVER;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD;MultipleActiveResultSets=True")
{
}
// DbSet, OnModelCreating, etc...
}
Run Code Online (Sandbox Code Playgroud)
确保在entityFramework部分之后添加连接字符串:
<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>
<connectionStrings>
<!-- your connection string goes here, after configSection -->
</connectionString>
Run Code Online (Sandbox Code Playgroud)小智 5
如果启动项目也发生改变的一个,它不具备连接字符串.
小智 5
这是因为您的上下文类是从 DbContext 继承的。我猜你的演员是这样的:
public MyEntities()
: base("name=MyEntities")
Run Code Online (Sandbox Code Playgroud)
name=...应该更改为您的连接字符串的名称
| 归档时间: |
|
| 查看次数: |
209089 次 |
| 最近记录: |