Atl*_*361 2 mysql linq-to-entities entity-framework-4 asp.net-mvc-3 connector-net
在2个不同的MySql安装上使用相同的MVC 3和C#代码以及相同MySql 5.5数据库的副本时.一个完美地工作,而另一个失败的"对象必须实现IConvertible"错误.当我针对本地PC的MySql安装运行查询时,查询总是运行良好,但是当我尝试从本地PC的Visual Studio 2010查询到我的Internet服务提供商的MySql安装中的同一数据库的副本时,我收到错误"对象必须实施IConvertible".我在代码中唯一更改的是web.config中的ConnectionString的Server Name,否则代码和数据库完全相同.注意:对ISP的MySql安装的所有其他查询完美地工作只有这一个特定查询不起作用.
在尝试对此进行故障排除几天后,我确信错误消息"对象必须实现IConvertible"与实际发生的事情无关,而且我的印象是我的ISP上的MySql配置可能会以某种方式导致此问题的原因如下:
如果这确实是代码中的"对象必须实现IConvertible"问题,那么无论我遇到哪个数据库安装,问题都会在代码中持续存在.
我已完全从我本地PC的工作副本中删除并重新创建了ISP的数据库,但仍然收到相同的错误.
最有趣的是,查询有3个嵌套的IEnumerable列表,这些列表都以与IEnumerables相同的方式设置,如果我在查询中注释掉这些嵌套列表中的任何一个,那么查询将成功针对ISP的DB运行.无论哪个嵌套列表被注释掉,只有2个或更少的嵌套列表然后查询到ISP的数据库工作.这就是我得出的结论,即ISP的MySql配置可能会以某种方式限制我的查询,因为我在查询本地PC的MySql安装时没有这个问题.错误消息似乎并不适用于实际发生的情况.同样,ISP的MySql安装的所有其他查询都能正常工作,但是它们都不包含超过2个嵌套的IEnumerable列表,其中此特定查询包含3个?
查询错误"对象必须实现IConvertible"
    Server Error in '/' Application.
    ________________________________________
    Object must implement IConvertible. 
    Description: An unhandled exception occurred during the execution of the current web request.             Please review the stack trace for more information about the error and where it originated in the code. 
    Exception Details: System.InvalidCastException: Object must implement IConvertible.
    Source Error: 
    Line 392:
    Line 393:
    Line 394:            var query =
    Line 395:                  (from p in db.products
    Line 396:                   where p.ClientId == clientId
    Source File: C:\Users\JR\Documents\Visual Studio 2010\Projects\Subversion\ReservarMVC\ReservarMVC\Models\ProductRepository.cs    Line: 394 
    Stack Trace: 
    [InvalidCastException: Object must implement IConvertible.]
    System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +9528453
    MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType) +566
    MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal) +231
    System.Data.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +215
    System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling(Int32 ordinal) +46
    lambda_method(Closure , Shaper ) +180
    System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +170
    System.Data.Common.Internal.Materialization.RowNestedResultEnumerator.MoveNext() +235
    System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.TryReadToNextElement() +49
    System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.ReadElement() +29
    System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.MoveNext() +68
    System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
    System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
    ReservarMVC.Models.ProductRepository.GetProductList(Nullable`1 dateStart, Nullable`1    dateEnd,         Nullable`1 personQuantityId, Nullable`1 roomQuantityId) in C:\Users\JR\Documents     \Visual      Studio 2010\Projects\Subversion\ReservarMVC\ReservarMVC\Models\ProductRepository.cs:394
    ReservarMVC.Models.ProductRepository.GetProductListSearch(Nullable`1 dateStart, Nullable`1 dateEnd, Nullable`1 personQuantityId, Nullable`1 roomQuantityId) in C:\Users\JR\Documents\Visual Studio 2010\Projects\Subversion\ReservarMVC\ReservarMVC\Models\ProductRepository.cs:605
    ReservarMVC.Controllers.PortalController.MyExcurcion(ProductListSearchVM viewModel) in C:\Users\JR\Documents\Visual Studio 2010\Projects\Subversion\ReservarMVC\ReservarMVC\Controllers\PortalController.cs:63
     lambda_method(Closure , ControllerBase , Object[] ) +162
     System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
      System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
      System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
      System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
      System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
      System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
      System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
      System.Web.Mvc.Controller.ExecuteCore() +116
      System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
      System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
      System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
      System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
      System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
      System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
      System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
      System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
      System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
      System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
      System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
      System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8967601
      System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
      ________________________________________
      Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.261
LINQ到实体查询:
    public IEnumerable<ProductListVM> GetProductList(DateTime? dateStart, DateTime? dateEnd, int? personQuantityId, int? roomQuantityId)
    {
        var query =
              (from p in db.products
               where p.ClientId == clientId
               where p.ProductTypeId == 5 
               where personQuantityId <= p.Capacity
               where roomQuantityId <= p.Quantity
               select new ProductListVM
               {
                   ProductId = p.ProductId,
                   ClientId = p.ClientId,
                   Name = p.Name_en,
                   Title = p.Title_en,
                   Description = p.Description_en,
                   Quantity = p.Quantity,
                   Capacity = p.Capacity,
                   newbookings = from b in db.bookings 
                                 where b.ProductId == p.ProductId
                                 from res in db.reservations
                                 where b.ReseravationId == res.ReservationId
                                 where b.BookingDateTime >= dateStart && b.BookingDateTime <= dateEnd && res.ReservationStateId != 3 
                                 select new BookingVM
                                 {
                                     ProductId = b.ProductId,
                                     BookingId = b.BookingId,
                                     ClientId = b.ClientId,
                                     ReservationId = b.ReseravationId,
                                     BookingDateTime = b.BookingDateTime
                                 },                  
                   newratecategories = from prc in db.productratecategories
                                       where prc.ProductId == p.ProductId
                                       from rc in db.ratecategories
                                       where prc.RateCategoryId == rc.RateCategoryId
                                       where (dateStart >= rc.DateStart && dateEnd <= rc.DateEnd) || (dateStart >= rc.DateStart || dateEnd <= rc.DateEnd)                                           
                                       select new MegaRateCategoryVM
                                       {
                                           RateCategoryId = rc.RateCategoryId,                                               
                                           DateStart = rc.DateStart,
                                           DateEnd = rc.DateEnd                                      
                                       },
                   newproductimages = (from pi in db.productimages
                                       where pi.ProductId == p.ProductId
                                       from ig in db.imagegalleries
                                       where pi.ImageGalleryId == ig.ImageGalleryId
                                       select new MegaProductImageVM
                                       {
                                           ig_Description = ig.ig_Description,
                                           ImageGalleryId = pi.ImageGalleryId
                                       }).OrderByDescending(i => i.ImageGalleryId)
               }).ToList();
        return query;
    }
产品列表ViewModel:
    namespace Project.ViewModels
    {
        [Serializable]
        public class ProductListVM : BaseViewModel
        {        
            public int ProductId { get; set; }        
            public int? ClientId { get; set; }
            public int? ProductTypeId { get; set; }
            public int? ProductStateId { get; set; }
            public String Name { get; set; }        
            public String Title { get; set; }
            public String Description { get; set; }
            public int? Quantity { get; set; }
            public int? Capacity { get; set; }        
            //PRODUCTIMAGE table fields
            public IEnumerable<MegaProductImageVM> newproductimages { get; set; }
            //RATECATEGORY table fields
            public IEnumerable<MegaRateCategoryVM> newratecategories { get; set; }
            //BOOKING table fields
            public IEnumerable<BookingVM> newbookings { get; set; }       
        }
    }
什么可能导致这种行为?这可能是由MySql 5.5的数据库配置中的查询阈值设置或网络设置引起的吗?我没有更改任何一个MySql 5.5安装的任何默认值.我只是使用标准的MySql Connector/Net 6.5.4来访问数据库以及Microsoft的标准MVC 3,C#,实体框架4,LINQ到实体代码.
非常感谢任何帮助.谢谢,Atlas361
要解决此问题,请在您的连接字符串中添加"尊重二进制标志= false",例如:
connectionString="metadata=res://*/Something.csdl|res://*/Something.ssdl|
res://*/Something.msl;provider=MySql.Data.MySqlClient;
provider connection string="server=my-server;User Id=some-user;
Persist Security Info=True;database=some-database;respect binary flags=false""
对于某些版本的MySQL 5.0和5.1,这是一个已知问题
在某些情况下,MySQL会返回有关一列或多列的错误元数据...如果您的应用程序所需的更改太大,则在您的连接字符串中添加"尊重二进制标志= false"会导致连接器使用先前的行为:任何标记为字符串的列,无论二进制标志如何,都将作为字符串返回.只有专门标记为BLOB的列才会返回为BLOB.
希望这有助于未来的旅行者
| 归档时间: | 
 | 
| 查看次数: | 1518 次 | 
| 最近记录: |