我正在开发一个应用程序,它将概述当日正在影院上映的电影。

我得到以下异常


  “ where”子句中的未知列“ Extent2.Movie_ID”


这是引发异常的地方(额外的变量movies2只是我调试时所做的事情):

public ActionResult MoviesToday()
{
    var todaysDate = Convert.ToDateTime(DateTime.Now).Date;
    var showsToday = db.Shows.Where(s => s.StartTime.Year == todaysDate.Year && s.StartTime.Month == todaysDate.Month && s.StartTime.Day == todaysDate.Day);
    var movies = from firstItem in db.Movies
                 join secondItem in showsToday
                      on firstItem equals db.Movies.Where(x => x == secondItem.Movie).FirstOrDefault()
                 select firstItem;
    movies = movies.Distinct();

    movies = movies.OrderBy(m => m.Name);
    var movies2 = movies.ToList(); // HERE THE EXCEPTION is THROWN
    return View(movies2);
}


这是我的Show模型类:

public class Show
{
        public int ID { get; set; }
        public DateTime StartTime { get; set; }
        public string BookingLink { get; set; }
        public string StartTimeAsString { get; set; }

        public Movie Movie { get; set; }
        public Theater Theater { get; set; }
    }


你看到什么错了吗?

编辑:完整的异常详细信息+ stacktrace:

System.Data.Entity.Core.EntityCommandExecutionException was unhandled by user code
  HResult=-2146232004
  Message=An error occurred while executing the command definition. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
       vid System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
       vid System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
       vid System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
       vid System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
       vid System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
       vid System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
       vid System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
       vid System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
       vid System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       vid System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       vid System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       vid CinemaWeb.Controllers.MoviesController.MoviesToday() i d:\Development\Cinema\CinemaWeb\Controllers\MoviesController.cs:rad 31
       vid lambda_method(Closure , ControllerBase , Object[] )
       vid System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       vid System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       vid System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       vid System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
       vid System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       vid System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       vid System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       vid System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
       vid System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       vid System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
       vid System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
  InnerException: MySql.Data.MySqlClient.MySqlException
       HResult=-2147467259
       Message=Unknown column 'Extent2.Movie_ID' in 'where clause'
       Source=MySql.Data
       ErrorCode=-2147467259
       Number=1054
       StackTrace:
            vid MySql.Data.MySqlClient.MySqlStream.ReadPacket()
            vid MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
            vid MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
            vid MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
            vid MySql.Data.MySqlClient.MySqlDataReader.NextResult()
            vid MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
            vid MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
            vid System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            vid System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
            vid System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
            vid System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
            vid System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
            vid System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            vid System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
       InnerException:


编辑3:我认为在Show和Movie之间建立映射的方式显然有问题,但是我发现的示例建议我以这种方式实现它。为了清楚起见,我希望一场演出有一部电影和一部剧院。

最佳答案

您的加入很“奇怪”。 equals如何评估someMovie == anotherMovie

你为什么不只是:

var movies = (from
    s in db.Shows
where
    s.StartTime.Year == todaysDate.Year &&
    s.StartTime.Month == todaysDate.Month &&
    s.StartTime.Day == todaysDate.Day
select
    s.Movie).Distinct().ToList();

var shows = (from
    s in db.Shows
where
    s.StartTime.Year == todaysDate.Year &&
    s.StartTime.Month == todaysDate.Month &&
    s.StartTime.Day == todaysDate.Day
select
    s).ToList();


要么

var shows = (from
    s in db.Shows.Include(x => x.Movies)
where
    s.StartTime.Year == todaysDate.Year &&
    s.StartTime.Month == todaysDate.Month &&
    s.StartTime.Day == todaysDate.Day
select
    s).ToList();

var movies = shows.Select(x => x.Movie).Distinct().ToList();

关于c# - Entity Framework :“where”子句中的“未知”列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33374564/

10-11 01:00