帮助EF Code第一个连接字符串

时间:2011-08-02 18:29:42

标签: entity-framework repository repository-pattern unit-of-work

我尝试使用this Scott Allen tutorial

实现UnitofWork模式

我当前的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);
        }
}

就是这个

enter image description here

2 个答案:

答案 0 :(得分:0)

我已将我的SqlRepository更改为:

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

}

我的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;
            }
        }..... rest of code here

如果这些修改不合适或打破其中一种模式,请纠正我

答案 1 :(得分:-1)

您使用的是ObjectContext而不是DbContextObjectContext使用EntityConnection及其System.Data.EntityClient提供商。它的连接字符串有different format