UnitOfWork.Drivers引发了类型为'System.InvalidOperationException'的异常

时间:2019-05-18 20:39:19

标签: .net core

我正在尝试创建三层.net核心应用程序,但对于我的第一个实体,它向我显示了此异常:Drivers ='UnitOfWork.Drivers'引发了类型为'System.InvalidOperationException'的异常

public class UnitOfWork : IDisposable
    {
        private readonly TLAppContext _context = new TLAppContext();

        private ITLAppRepository<Driver> _drivers;

        public TLAppContext Context { get { return _context; } }
        public ITLAppRepository<Driver> Drivers { get { return _drivers ?? (_drivers = new TLAppRepository<Driver>(_context)); } }

        public void Commit()
        {
            _context.SaveChanges();
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
public class DriversController : BaseController
    {
        // GET: api/Drivers
        [HttpGet]
        public IActionResult Get()
        {
            try
            {
                return Ok(UnitOfWork.Drivers.Get().ToList().Select(x => Factory.Create(x)).ToList());
            }
            catch (Exception ex)
            {
                return BadRequest();
            }

        }
blic class BaseController : ControllerBase
    {
        private UnitOfWork _unitOfWork;
        private Factory _factory;
        //private SetOfReports _reports;

        protected UnitOfWork UnitOfWork
        {
            get { return _unitOfWork ?? (_unitOfWork = new UnitOfWork()); }
        }

        protected Factory Factory
        {
            get { return _factory ?? (_factory = new Factory(UnitOfWork)); }
        }

 public class Factory
    {
        private UnitOfWork _unitOfWork;
        public Factory(UnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        public DriverModel Create(Driver driver)
        {
            return new DriverModel()
            {
                ID = driver.ID,
                firstName = driver.firstName,
                lastName = driver.lastName,
                jmbg = driver.jmbg
            };
        }

        public Driver Create(DriverModel model)
        {
            return new Driver()
            {
                ID = model.ID,
                firstName = model.firstName,
                lastName = model.lastName,
                jmbg = model.jmbg
            };
        }
    }
 public class DriverModel
    {
        public int ID { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public long jmbg { get; set; }
    }
public class Driver
    {
        public int ID { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public long jmbg { get; set; }
    }
public interface ITLAppRepository<Entity>
    {
        Entity Get(int id);
        IQueryable<Entity> Get();
        void Insert(Entity entity);
        void Update(Entity entity, int id);
        void Delete(int id);
        bool Commit();

    }
 public class TLAppRepository<Entity> : ITLAppRepository<Entity> where Entity: class
    {
        protected TLAppContext context;
        protected DbSet<Entity> dbSet;

        public TLAppRepository(TLAppContext _context)
        {
            context = _context;
            dbSet = context.Set<Entity>();
        }

        public virtual IQueryable<Entity> Get()
        {
            return dbSet;
        }

        public Entity Get(int id)
        {
            return dbSet.Find(id);
        }

        public virtual void Insert(Entity entity)
        {
            dbSet.Add(entity);
        }

        public virtual void Update(Entity entity, int id)
        {
            Entity oldEntity = Get(id);
            if (oldEntity != null)
            {
                context.Entry(oldEntity).CurrentValues.SetValues(entity);
            }
        }

        public void Delete(int id)
        {
            Entity oldEntity = Get(id);
            if (oldEntity != null)
            {
                dbSet.Remove(oldEntity);
            }
        }

        public bool Commit()
        {
            return (context.SaveChanges() > 0);
        }
    }

它应该返回给我两个我在db中拥有的驱动程序,我已经尝试使用通用.net核心控制器并且它起作用了。

0 个答案:

没有答案