有关 EF Code First 连接字符串的帮助
实现 UnitofWork 模式
我正在尝试使用 此 Scott Allen 教程我当前的 SqlUnitOfWork 如下
public class SqlUnitOfWork : IUnitOfWork {
public SqlUnitOfWork() {
var connectionString =
ConfigurationManager
.ConnectionStrings[ConnectionStringName]
.ConnectionString;
_context = new ObjectContext(connectionString);
_context.ContextOptions.LazyLoadingEnabled = true;
}
public IRepository<PhysicalTest> PhysicalTests
{
get {
if (_physicalTests == null)
{
_physicalTests = new SqlRepository<PhysicalTest>(_context);
}
return _physicalTests;
}
}
public IRepository<EHR> EHRs
{
get
{
if (_EHRs == null)
{
_EHRs = new SqlRepository<EHR>(_context);
}
return _EHRs;
}
}
public void Commit() {
_context.SaveChanges();
}
SqlRepository<PhysicalTest> _physicalTests = null;
SqlRepository<EHR> _EHRs = null;
readonly ObjectContext _context;
const string ConnectionStringName = "default";
}
,我当前的连接字符串如下
<add name="default" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;MultipleActiveResultSets=True; initial catalog=MyAppDB" providerName="System.Data.SqlClient" />
还值得指出的是,我使用使用 mvcscaffolding 创建的控制器的控制器工作正常,但工作单元(由于某种原因需要一个连接字符串作为参数,而不是仅仅使用 MyAppDBContext() 实例)不起作用。
当我尝试使用以下代码调用 controllr 内的操作时出现错误:
public class PhysicalTestsController : Controller
{
private IUnitOfWork unitOfWork;
private IRepository<EHR> ehrRepository;
public PhysicalTestsController(IUnitOfWork unit)
{
unitOfWork = unit;
ehrRepository = unitOfWork.EHRs;
}
public ActionResult Index(int ehrId, int? page)
{
EHR ehr = ehrRepository.FindById(ehrId);
if (ehr.UserName != User.Identity.Name)
return View("Invalid Owner");
const int pageSize = 5;
var physicaltests = ehr.PhysicalTests.OrderByDescending(test => test.CreationDate);
List<PhysicalTestListItem> physicalTestsVM = new List<PhysicalTestListItem>();
Mapper.Map(physicaltests, physicalTestsVM);
var paginatedTests = new PaginatedList<PhysicalTestListItem>(physicalTestsVM, page ?? 0, pageSize);
return View(paginatedTests);
}
}
是这个
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial
My current SqlUnitOfWork is the folowing
public class SqlUnitOfWork : IUnitOfWork {
public SqlUnitOfWork() {
var connectionString =
ConfigurationManager
.ConnectionStrings[ConnectionStringName]
.ConnectionString;
_context = new ObjectContext(connectionString);
_context.ContextOptions.LazyLoadingEnabled = true;
}
public IRepository<PhysicalTest> PhysicalTests
{
get {
if (_physicalTests == null)
{
_physicalTests = new SqlRepository<PhysicalTest>(_context);
}
return _physicalTests;
}
}
public IRepository<EHR> EHRs
{
get
{
if (_EHRs == null)
{
_EHRs = new SqlRepository<EHR>(_context);
}
return _EHRs;
}
}
public void Commit() {
_context.SaveChanges();
}
SqlRepository<PhysicalTest> _physicalTests = null;
SqlRepository<EHR> _EHRs = null;
readonly ObjectContext _context;
const string ConnectionStringName = "default";
}
and my current connection string is the following
<add name="default" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;MultipleActiveResultSets=True; initial catalog=MyAppDB" providerName="System.Data.SqlClient" />
It's also worth pointing out that my controllers that are using controllers that were created with mvcscaffolding work fine but the unit of work (which for some reason needs a connectrion string as parameter instead of just using MyAppDBContext() instance) doesnt work.
The error I get when I try to invoke an action inside a controllr with the following code:
public class PhysicalTestsController : Controller
{
private IUnitOfWork unitOfWork;
private IRepository<EHR> ehrRepository;
public PhysicalTestsController(IUnitOfWork unit)
{
unitOfWork = unit;
ehrRepository = unitOfWork.EHRs;
}
public ActionResult Index(int ehrId, int? page)
{
EHR ehr = ehrRepository.FindById(ehrId);
if (ehr.UserName != User.Identity.Name)
return View("Invalid Owner");
const int pageSize = 5;
var physicaltests = ehr.PhysicalTests.OrderByDescending(test => test.CreationDate);
List<PhysicalTestListItem> physicalTestsVM = new List<PhysicalTestListItem>();
Mapper.Map(physicaltests, physicalTestsVM);
var paginatedTests = new PaginatedList<PhysicalTestListItem>(physicalTestsVM, page ?? 0, pageSize);
return View(paginatedTests);
}
}
is this one
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已将我的 SqlRepository 更改为:
以及
我的 SqlUnitofWork,
如果此修改不合适或破坏了这些模式之一,请纠正我
I have changed my SqlRepository to:
and
my SqlUnitofWork to
please correct me if this modifications are not appropriate or break one of these patterns
您使用的是
ObjectContext
而不是DbContext
。ObjectContext
使用EntityConnection
及其System.Data.EntityClient
提供程序。它的连接字符串具有不同的格式。You are using
ObjectContext
notDbContext
.ObjectContext
usesEntityConnection
and itsSystem.Data.EntityClient
provider. It's connection string has different format.