【问题标题】:Help with EF Code first connection stringEF 代码第一个连接字符串的帮助
【发布时间】:2011-08-02 18:29:41
【问题描述】:

我正在尝试使用 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() 实例)不起作用。

当我尝试使用以下代码在控制器中调用操作时遇到的错误:

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);
        }
}

是这个吗

【问题讨论】:

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


    【解决方案1】:

    我已将我的 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
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢您的回答....您介意看看我提供的答案吗??...我只是想找出实现工作单元模式的最佳方法...我想我的问题应该更清楚.. 我仍然不确定是否应该保留我在问题中提供的代码,只是更改连接字符串或使用我的答案中使用 DbContext 的代码....
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多