【发布时间】:2014-12-26 21:43:38
【问题描述】:
我创建了一个名为 Repository.EF 的项目来处理 n 轮胎解决方案中的数据访问。我已将 EF 添加到我拥有所有 POCO 的 Repository.EF 项目中。然后我像这样在那个项目中创建了一个 DbContext 类。
namespace LearningSpike.Repositories.EF
{
class GlassContractDbContext:DbContext
{
public GlassContractDbContext() : base("GlassContractContext")
{
}
public DbSet<MetalStock> MetalStock { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new MetalStockConfiguration());
}
}
然后转到包管理器控制台并做了
Enable-Migrations
然后设置
AutomaticMigrationsEnabled = true;
然后
Update-Database
一切正常。但问题是,我不知道连接字符串在哪里。该特定项目中似乎没有 connectionString 。我知道如果我有一个 MVC4/5 模板,web.config 中就会有一个 connectionString。如何找到连接字符串? 我现在该如何配置?例如,我记得在 MVC5 应用程序中使用 connectionString 进行此操作
MultipleActiveResultSets=true
我现在该怎么做?
谢谢! 干杯!
PS
我在 Repository.EF 项目的 App.config 中也有以下代码
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit
http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,
EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
【问题讨论】:
-
你的连接字符串应该在你的 web 项目的 web.config 中
-
您好,感谢您的快速回复。但是,问题是,这个项目中没有 web.config。这个项目只是一个类库,我有我所有的 POCO 的 .所以我在其中添加了 EF。我们的想法是保持这一层分离并仅将其用于数据访问。
-
您单独的类库将被加载到某些应用程序中以便工作,因此它将使用来自其“主机”应用程序的连接字符串。
-
当您创建一个库并且该库被另一个项目引用时,EF 将读取与启动项目相关的配置。否则你有一个 app.config 里面有连接字符串,否则应该在你的 datacontext ctor
-
我在这里添加了我的 app.config。请看看并告诉我是否可以在这里添加我的连接字符串?
标签: asp.net-mvc entity-framework model-view-controller