Help with EF Code first connection string

与世无争的帅哥 提交于 2020-01-03 03:38:10

问题


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


回答1:


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




回答2:


You are using ObjectContext not DbContext. ObjectContext uses EntityConnection and its System.Data.EntityClient provider. It's connection string has different format.



来源:https://stackoverflow.com/questions/6916785/help-with-ef-code-first-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!