有关 EF Code First 连接字符串的帮助

发布于 2024-11-27 07:58:42 字数 2958 浏览 3 评论 0原文

实现 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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘和柠 2024-12-04 07:58:42

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

如果此修改不合适或破坏了这些模式之一,请纠正我

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

葬シ愛 2024-12-04 07:58:42

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文