使用UnitOfWork从viewmodel返回列表

问题描述 投票:0回答:1

我尝试用unitOFWOrk和genericRepository创建两个列表。我有一个对象Climb,它存在于一个文本字段(你可以填写攀登的名字)和两个列表:Country:你可以在哪里选择一个国家和一个列表:difficultGrade ,在那里你可以选择一个艰难的爬坡等级。

这是我创造攀登的方法:

  public ActionResult Create()
        {

            var queryCountry = unitOfWork.ClimbRepository.Get(
              orderBy: q => q.OrderBy(r => r.country.Climbs));
            var queryDifficult = unitOfWork.ClimbRepository.Get(
                orderBy: qd => qd.OrderBy(d => d.difficult.Climbs));

            ViewBag.CountryId = new SelectList( queryCountry, "CountryId", "country_name");
            ViewBag.DifficultId = new SelectList(queryDifficult, "DifficultId", "DifficultName");
            return View();
        }

这是Generic存储库:

 public class GenericRepository<TEntity> where TEntity : class
    {
        internal LolaBikeContext context;
        internal DbSet<TEntity> dbSet;

        public GenericRepository(LolaBikeContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
        {
            return dbSet.SqlQuery(query, parameters).ToList();
        }

        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

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

        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }

        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }

        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }
    }

UnitOFWork类:

public class UnitOfWork : IDisposable
    {
        private LolaBikeContext context = new LolaBikeContext ();
        private GenericRepository<UserProfile> userRepository;
        private GenericRepository<Route> routeRepository;
        private GenericRepository<Climb> climbRepository;
        private GenericRepository<Country> countryRepository;

        public GenericRepository<UserProfile> UserRepository
        {


            get
            {

                if (this.userRepository == null)
                {
                    this.userRepository = new GenericRepository<UserProfile>(context);
                }
                return userRepository;
            }
        }

        public GenericRepository<Route> RouteRepository
        {
            get
            {
                if (this.routeRepository == null)
                {
                    this.routeRepository = new GenericRepository<Route>(context);
                }
                return routeRepository;
            }
        }

        public GenericRepository<Climb>ClimbRepository
        {
            get
            {

                if (this.climbRepository == null)
                {
                    this.climbRepository = new GenericRepository<Climb>(context);
                }
                return this.climbRepository;
            }

        }


        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

和我的viewModel:

public class ClimbViewModel
    {

        public ClimbViewModel()
        {
            Countries = new List<SelectListItem>();

        }
        public int Id { get; set; }

        [Required(ErrorMessage="You need to give it a name")]
        public string Name { get; set; }
        public int SelectedValue { get; set; }

        public int SelectedCountryId { get; set; }       
        public IEnumerable<SelectListItem> Countries { get; set; }
        public IEnumerable<SelectListItem> DifficultGrades {get; set; }

    }

问题当然是我如何获得国家名单和困难等级列表?

谢谢

在过去,我有这样的:

   ViewBag.CountryId = new SelectList(db.countries, "CountryId", "country_name");
            ViewBag.DifficultId = new SelectList(db.difficults, "DifficultId", "DifficultName");

但所以我用GenericRepository和UnitOfWork替换它们。但如果我这样做就像在这里做的那样。我收到此错误:

A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The entity type ClimbViewModel is not part of the model for the current context.



public ActionResult Create()
        {



            var queryCountry = unitOfWork.ClimbRepository.Get(
              orderBy: q => q.OrderBy(r => r.country.Climbs));
            var queryDifficult = unitOfWork.ClimbRepository.Get(
                orderBy: qd => qd.OrderBy(d => d.difficult.Climbs));

            ViewBag.CountryId = new SelectList( queryCountry, "CountryId", "country_name");
            ViewBag.DifficultId = new SelectList(queryDifficult, "DifficultId", "DifficultName");
            return View();
        }

当然是错的。但我不知道如何改变这一点。我得到了这个错误:

An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code

Additional information: DbSortClause expressions must have a type that is order comparable.

这是堆栈跟踪:

   at System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateSortClause(DbExpression key)
   at System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.ToSortClause(DbExpression key)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OrderByTranslatorBase.TranslateOneLambda(ExpressionConverter parent, DbExpressionBinding sourceBinding, DbExpression lambda)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
   at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ContosoUniversity.DAL.GenericRepository`1.Get(Expression`1 filter, Func`2 orderBy, String includeProperties) in d:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\DAL\GenericRepository.cs:line 46
   at ContosoUniversity.Controllers.ClimbController.Create() in d:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Controllers\ClimbController.cs:line 68
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()

好的,我现在就像这样:

public ActionResult Create()
        {

            var queryCountry = unitOfWork.CountryRepository.Get(orderBy: q => q.OrderBy(c =>c.country_name));
            var queryDifficult = unitOfWork.DifficultRepository.Get(orderBy: q => q.OrderBy(d => d.DifficultName));


            ViewBag.CountryId = new SelectList( queryCountry, "CountryId", "country_name");
            ViewBag.DifficultId = new SelectList(queryDifficult, "DifficultId", "DifficultName");
            return View();
        }

所以我的UnitOfwork现在看起来像这样:

public class UnitOfWork : IDisposable
    {
        private LolaBikeContext context = new LolaBikeContext ();
        private GenericRepository<UserProfile> userRepository;
        private GenericRepository<Route> routeRepository;
        private GenericRepository<Climb> climbRepository;
        private GenericRepository<Country> countryRepository;
        private GenericRepository<Difficult> difficultRepository;
        private GenericRepository<ClimbViewModel> climbViewModelRepository;


        public GenericRepository<UserProfile> UserRepository
        {


            get
            {

                if (this.userRepository == null)
                {
                    this.userRepository = new GenericRepository<UserProfile>(context);
                }
                return userRepository;
            }
        }

        public GenericRepository<Route> RouteRepository
        {
            get
            {
                if (this.routeRepository == null)
                {
                    this.routeRepository = new GenericRepository<Route>(context);
                }
                return routeRepository;
            }
        }

        public GenericRepository<Climb>ClimbRepository
        {
            get
            {

                if (this.climbRepository == null)
                {
                    this.climbRepository = new GenericRepository<Climb>(context);
                }
                return this.climbRepository;
            }

        }

        public GenericRepository<ClimbViewModel> ClimbViewModelRepository 
        {
            get
            {
                if (climbViewModelRepository == null)
                {
                    this.climbViewModelRepository = new GenericRepository<ClimbViewModel>(context);
                }
                return this.climbViewModelRepository;

            }
        }

        public GenericRepository<Country>CountryRepository
        {

            get
            {

                if (this.countryRepository != null)
                {
                    countryRepository = new GenericRepository<Country>(context);
                }
                return this.countryRepository;
            }
        }

        public GenericRepository<Difficult>DifficultRepository
        {


            get
            {

                if (difficultRepository != null)
                {
                    difficultRepository = new GenericRepository<Difficult>(context);
                }
              return  this.difficultRepository;
            }

        }




        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

但现在我收到这个错误:

A first chance exception of type 'System.NullReferenceException' occurred in ContosoUniversity.dll

Additional information: Object reference not set to an instance of an object.

我现在这样:

 var queryCountry = unitOfWork.ClimbRepository.Get(
             orderBy: q => q.OrderBy(r => r.country.country_name));

            var queryDifficult = unitOfWork.ClimbRepository.Get(
                orderBy: qd => qd.OrderBy(d => d.difficult.DifficultName));

但后来我得到了这个错误:

A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll

Additional information: DataBinding: 'System.Data.Entity.DynamicProxies.Climb_0568B2C1C64C1C347E3DF33E87F37C8D672D9794887F77C523EB7B16E9399FCE' does not contain a property with 

在这条线上:

 <div class="form-group">

            <label class="control-label col-md-2" for="CountryID">Country</label>
            @Html.DropDownList("CountryId", ViewBag.countries as SelectList, new { @class = "form-control", style = "width: 250px;" })
        </div>

这是我的国家班:

 public class Country
    {

        //[Key]
        public int CountryId { get; set; }
        public string country_code { get; set; }

        [DisplayName("Country")]
        public string country_name { get; set; }



        public virtual ICollection<Climb> Climbs { get; set; }
        public virtual ICollection<Route> Routes { get; set; }
    }

和我的Climb课程:

public class Climb
    {

        [Key]
        public int climbID { get; set; }     
        public string Name { get; set; }
        public int Points { get; set; }


        public int? UserProfileID { get; set; }
        public int? CountryId { get; set; }
        public int? DifficultId { get; set; }



        public virtual UserProfile userProfile { get; set; }
        public virtual Country country { get; set; }
        public virtual Difficult difficult { get; set; } 




    }

这工作:

  ViewBag.CountryId = new SelectList(db.countries, "CountryId", "country_name");
            ViewBag.DifficultId = new SelectList(db.difficults, "DifficultId", "DifficultName");

但是如何用unitOfWork重写呢?

谢谢

c# asp.net-mvc entity-framework asp.net-mvc-4 unit-of-work
1个回答
0
投票

您尝试添加的逻辑属于Model而不是Repository,存储库与它无关。负责的存储库是访问数据库并对其运行查询。

我写了一篇文章,介绍如何使用EF规划Thin存储库和工作单元

我将在https://www.codeproject.com/Articles/1157241/Very-Thin-Database-Layer-using-UnitOfWork-Pattern下面插入链接

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.