异常堆栈
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'SdVipServer.SdModels.SdDBContext'.
System.InvalidOperationException: Nullable object must have a value.
at lambda_method879(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
System.InvalidOperationException: Nullable object must have a value.
at lambda_method879(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
fail: SdVipServer.Program[0]
An error occurred creating the DB.
System.InvalidOperationException: Nullable object must have a value.
at lambda_method879(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
报错的LINQ代码
from t in dbContext.LTraders
join p in dbContext.DShops on t.Shopid equals p.Shopid into result1
from row1 in result1.DefaultIfEmpty()
join d in dbContext.DsDistributors on t.Traderid equals d.Clientid into result2
from row2 in result2.DefaultIfEmpty()
where (t.Traderid > 0) && (row2.Status == null || row2.Status == 1)
orderby t.Code
select new {
t.Traderid, t.Shipto, t.Scamt, t.Code, t.Name, t.Areaid,
t.Countryid, t.Isvendor, t.Isclient, t.Isother, t.Credit, t.Creditday,
t.Closed, t.Suramt, t.Speedsymbol, t.Iscashtrader, t.Shopid, t.Acctrader,
t.Paymethodid, t.TaxRate,
Shopname = row1.Name ?? "",
t.Moneyid, t.Empid, t.Departmentid,
t.Isexpress, Distributorid =(row2.Distributorid == null ? 0 : row2.Distributorid), row2.Kind ,
t.Contactor
};
row2.Kind 的类型信息是:
public int Kind { get; set; }
因为数据库是空的
row2.Kind的值是null
改成 Kind = row2.Kind == null ? 0 : row2.Kind
或者 public int? Kind { get; set; }
都可以解决此异常