SqlSugar.DbBindAccessory.SetOwnsOne时,值对象为空值报错。 返回
SqlSugar
处理完成
5
174
良士休休 发布于1个月前
悬赏:0 飞吻
在SqlSugar.DbBindAccessory.SetOwnsOne中能不能加一个空值的处理,不然会出错。
private void SetOwnsOne(object addItem, bool isOwnsOne, EntityInfo entityInfo, IDataReader dataReader) { if (isOwnsOne) { var ownsOneColumnsKv = entityInfo.Columns.Where(it => it.ForOwnsOnePropertyInfo != null) .GroupBy(it => it.ForOwnsOnePropertyInfo).ToList(); foreach (var kv in ownsOneColumnsKv) { var parentObj = kv.Key.GetValue(addItem); if (parentObj == null) { parentObj = kv.Key.PropertyType.Assembly.CreateInstance(kv.Key.PropertyType.FullName); kv.Key.SetValue(addItem, parentObj); } foreach (var item in kv.ToList()) { var itemIndex = dataReader.GetOrdinal(item.DbColumnName); var isDBNull = Convert.IsDBNull(dataReader.GetValue(itemIndex)); item.PropertyInfo.SetValue(parentObj, isDBNull ? null : dataReader.GetValue(itemIndex)); } } } }
item in kv.ToList,值对象的item明明设置成string?,dataReader.GetValue(itemIndex)值为null, item.PropertyInfo.SetValue(parentObj, dataReader.GetValue(itemIndex))会出错,报null无法转换成string。
热忱回答(5)
-
fate sta VIP01个月前
提供可以理现的DEMO
0 回复 -
良士休休 VIP01个月前
https://www.donet5.com/Home/Doc?typeId=2576
官方教程里面,
public class UnitAddressadfafa { //支持SugarColumn设置别名 public string Street { get; set; } public string City { get; set; } public string ZipCode { get; set; } }
改成
public class UnitAddressadfafa { //支持SugarColumn设置别名 public string Street { get; set; } public string? City { get; set; } public string ZipCode { get; set; } }
就是会报错。。可是我具体的业务里,就是希望它可空没问题。
0 回复 -
fate sta VIP01个月前
@良士休休:需要DEMO一定要DEMO完整的。不然没有意义 。string?和string没有差异只是编译前的差异
0 回复 -
良士休休 VIP01个月前
using SqlSugar; using DbType = SqlSugar.DbType; namespace OrmTest { internal class Program { private static void Main(string[] args) { var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() { ConnectionString = "Server=.;Database=Test;User=sa;Password=123456;MultipleActiveResultSets=True;TrustServerCertificate=True;", DbType = DbType.SqlServer, IsAutoCloseConnection = true }); db.CodeFirst.InitTables<UnitCustomeradfafas>(); db.DbMaintenance.TruncateTable<UnitCustomeradfafas>(); var list = db.Queryable<UnitCustomeradfafas>().ToList(); if (list.Count == 0) { db.Insertable(new UnitCustomeradfafas() { CustomerId = 1, Name = "name", Address = new UnitAddressadfafa() { City = null, Street = "street", ZipCode = "zipCode" } }).ExecuteCommand(); } var list1 = db.Queryable<UnitCustomeradfafas>().ToList(); Console.WriteLine("用例跑完"); Console.ReadKey(); } //建类 public class UnitAddressadfafa { //支持SugarColumn设置别名 public string Street { get; set; } [SqlSugar.SugarColumn(IsNullable = true)] public string? City { get; set; } public string ZipCode { get; set; } } public class UnitCustomeradfafas { [SqlSugar.SugarColumn(IsPrimaryKey = true)] public int CustomerId { get; set; } [SugarColumn(IsNullable = true)] public string? Name { get; set; } [SqlSugar.SugarColumn(IsOwnsOne = true/*, IsNullable = true*/)] public UnitAddressadfafa Address { get; set; } } } }
写了个测试,但是没有复现那个BUG,这.......
0 回复 -
良士休休 VIP01个月前
这个问题,之前已经解决了,是我的版本太旧,没更新,升级到5.1.4.166就好了,不好意思。。
0 回复