简体   繁体   English

实体框架错误-由于实例化值为null,因此强制转换为值类型'System.Int64'

[英]Entity Framework Error - The cast to value type 'System.Int64' failed because the materialized value is null

The below method fails when Execting ToListAsync() with the following error 当使用以下错误执行ToListAsync()时,以下方法失败

The cast to value type 'System.Int64' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. 

-- -

 private IQueryable<MaterialSearchResult> MaterialSearchQuery(string searchTerm)
            {    
                return from m in this.context.GB_Material
                       from v in this.context.WF_VideoVersion
                           .Where(
                               v =>
                                   v.MaterialID == m.MaterialID)
                           .DefaultIfEmpty()
                       select new MaterialSearchResult
                       {
                           MaterialId = v.MaterialID,
                           MaterialName = v.GB_Material.MaterialName,
                           MaterialTitle = v.GB_Material.MaterialTitle,
                           CreatedDate = v.GB_Material.CreatedDate,
                           NearestTxDate = v.NearestTXDate,
                           Channel = v.ScheduleName
                       };
    }

Calling method 调用方式

public Task<List<MaterialSearchResult>> SearchMaterials(string searchTerm, string sort, string direction, int pageSize = 20, int skip = 0)
    {
        var data = MaterialSearchQuery(searchTerm)
            .SortMaterials(sort,direction)
            .Skip(skip)
            .Take(pageSize);

        return data.ToListAsync();
    }

public class MaterialSearchResult
{
    public long MaterialId { get; set; }
    public string MaterialName { get; set; }
    public string MaterialTitle { get; set; }
    public DateTime? NearestTxDate { get; set; }
    public string Channel { get; set; }
    public DateTime CreatedDate { get; set; }
}

 public class GB_Material()
    {
       public long MaterialID { get; set; }
    public string MaterialName { get; set; }
    public string MaterialTitle { get; set; }
    public System.DateTime CreatedDate { get; set; }

   public virtual ICollection<WF_VideoVersion> WF_VideoVersion { get; set; }
    }

     public class WF_VideoVersion()
    {
         public long VideoVersionID { get; set; }
         public long MaterialID { get; set; }
         public Nullable<System.DateTime> NearestTXDate { get; set; }
         public string ScheduleName { get; set; }

         public virtual GB_Material GB_Material { get; set; }

    }

EDIT: 编辑:

I'm also getting the same error on the NearestTxDate property 我在NearestTxDate属性上也遇到相同的错误

The cast to value type 'System.DateTime' failed because the materialized value is null. 

Try to rewrite your query: 尝试重写您的查询:

return from m in this.context.GB_Material
                       from v in this.context.WF_VideoVersion
                           .Where(
                               v =>
                                   v.MaterialID == m.MaterialID)
                           .DefaultIfEmpty()
                       select new MaterialSearchResult
                       {
                           MaterialId = m.MaterialID,
                           MaterialName = m.MaterialName,
                           MaterialTitle = m.MaterialTitle,
                           CreatedDate = m.CreatedDate,
                           NearestTxDate = v.NearestTXDate,
                           Channel = v.ScheduleName
                       };

For some insane reason i had written the select statement like this: 由于某些疯狂的原因,我这样写了select语句:

                  select new MaterialSearchResult
                   {
                       MaterialId = v.MaterialID,
                       MaterialName = v.GB_Material.MaterialName,
                       MaterialTitle = v.GB_Material.MaterialTitle,
                       CreatedDate = v.GB_Material.CreatedDate,
                       NearestTxDate = v.NearestTXDate,
                       Channel = v.ScheduleName
                   }

WHen it should have been this 为什么应该是这个

 select new MaterialSearchResult
                       {
                           MaterialId = mMaterialID,
                           MaterialName = m.MaterialName,
                           MaterialTitle = m.MaterialTitle,
                           CreatedDate = m.CreatedDate,
                           NearestTxDate = v.NearestTXDate,
                           Channel = v.ScheduleName
                       }

Given that for some results the child would be null, then obviously going through the child to get the parents properties is not going to work. 考虑到对于某些结果,孩子将为空,那么显然要通过孩子来获取父母的财产是行不通的。

I'm goin to blame it on a pre-lunch brain fart. 我要把它归咎于午餐前放屁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:强制转换为值类型&#39;System.Int32&#39;的原因是物化值为null - Error: The cast to value type 'System.Int32' failed because the materialized value is null 转换为值类型&#39;Int64&#39;失败,因为实例化值为null - The cast to value type 'Int64' failed because the materialized value is null 强制转换为值类型&#39;System.Int32&#39;,因为实例化值为null。 - The cast to value type 'System.Int32' failed because the materialized value is null. LINQ:转换为值类型&#39;System.Int32&#39;失败,因为具体化值为null - LINQ: The cast to value type 'System.Int32' failed because the materialized value is null 完全外连接:由于物化值为空,因此转换为值类型“System.Int32”失败 - Full outer join: The cast to value type 'System.Int32' failed because the materialized value is null 转换为值类型&#39;System.Int32&#39;失败,因为实例化值为null - The cast to value type 'System.Int32' failed because the materialized value is null 错误:由于实例化值为空,因此强制转换为值类型“ Double” - Error :the cast to value type “Double” failed because the materialized value is null Linq错误:强制转换为值类型&#39;Int32&#39;,因为物化值为null - Linq error: The cast to value type 'Int32' failed because the materialized value is null 将值“null”转换为类型“System.Nullable`1[System.Int64]”时出错 - Error converting value "null" to type 'System.Nullable`1[System.Int64]' 从物化的“System.Int32”类型到“System.Int64”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM