帮助EF Code第一个连接字符串 [英] Help with EF Code first connection string

查看:146
本文介绍了帮助EF Code第一个连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用这个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 =defaultconnectionString =data source = .\SQLEXPRESS; Integrated Security = SSPI; MultipleActiveResultSets = True; initial catalog = MyAppDBproviderName =System.Data.SqlClient/> 

还值得指出的是,使用通过mvcscaffolding创建的控制器的控制器工作正常,但是工作单位(由于某种原因需要连接字符串作为参数,而不是仅使用MyAppDBContext()实例)does not work。



当我尝试调用时,我得到的错误控制器内的一个动作,具有以下代码:

  public class PhysicalTestsController:Controller 
{
private IUnitOfWork的UnitOfWork;
private IRepository< EHR> ehrRepository;


public PhysicalTestsController(IUnitOfWork unit)
{
unitOfWork = unit;
ehrRepository = unitOfWork.EHRs;
}


public ActionResult索引(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);
列表< PhysicalTestListItem> physicalTestsVM = new List< PhysicalTestListItem>();
Mapper.Map(physicaltests,physicalTestsVM);
var paginatedTests = new PaginatedList< PhysicalTestListItem>(physicalTestsVM,page?0,pageSize);
return View(paginatedTests);
}
}

这是一个



解决方案

我已将我的SqlRepository更改为:

  public class SqlRepository< T> :IRepository< T> 
其中T:class,IEntity {

内部SummumnetDB上下文;
内部DbSet< T> _objectSet;

public SqlRepository(SummumnetDB context)
{
this.context = context;
this._objectSet = context.Set< T>();
}

.....其余的方法在这里

}



我的SqlUnitofWork到

  public class SqlUnitOfWork:IUnitOfWork {

private SummumnetDB _context = new SummumnetDB();

public IRepository< PhysicalTest> PhysicalTests
{
get {
if(_physicalTests == null)
{
_physicalTests = new SqlRepository< PhysicalTest>(_ context);
}
return _physicalTests;
}
} .....其余的代码在这里

请更正如果这种修改不合适,或者打破这些模式之一,那么我就是

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

解决方案

I have changed my SqlRepository to:

public class SqlRepository<T> : IRepository<T>
                                    where T : class, IEntity {

        internal SummumnetDB context;
        internal DbSet<T> _objectSet;

        public SqlRepository(SummumnetDB context)
        {
            this.context = context;
            this._objectSet = context.Set<T>();
        }

..... rest of my methods here

}

and

my SqlUnitofWork to

public class SqlUnitOfWork : IUnitOfWork {

        private SummumnetDB _context = new SummumnetDB();

        public IRepository<PhysicalTest> PhysicalTests
        {
            get {
                if (_physicalTests == null)
                {
                    _physicalTests = new SqlRepository<PhysicalTest>(_context);
                }
                return _physicalTests;
            }
        }..... rest of code here

please correct me if this modifications are not appropriate or break one of these patterns

这篇关于帮助EF Code第一个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆