自动创建数据库和表之后,使用事务插入多张表出错 Object does not match target type. 返回

SqlSugar 老数据
4 1974
  1. 自动创建数据库和两张表 t_product 和 t_product_model

  2. 模型如下:

  3. image.png

  4. image.png

  5. startup.cs 中写入自动创建数据库和表的方法

  6. image.png

  7. 进入建表的方法

  8. image.png

  9. 自动建库建表完成,好使,没出问题,而且修改表和字段,字段的增删都好使。

  10. 创建简单的仓储

  11. image.png

  12. ProductRepostory 产品仓储 继承仓储基类,两个简单的方法

  13. image.png

  14. ProductModelRepostory 仓储继承了 仓储基类 一个简单的添加方法

  15. image.png

  16. 执行添加功能是要往两张表添加数据

  17. image.png

  18. 由于程序出错,没有做事务的提交和回滚,数据库表中都没有数据。

  19. 自己经过多种测试,不是DB问题,只要不执行自动创建数据库和表,执行添加就不会出错,感觉是最后一张表没有放到映射集合中,导致添加表数据就找不到映射类。大神们来帮帮忙。

  20. 错误报告:

  21. System.Reflection.TargetException: Object does not match target type.

  22.    at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)

  23.    at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

  24.    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

  25.    at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)

  26.    at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)

  27.    at SqlSugar.InsertableProvider`1.SetInsertItemByEntity(Int32 i, T item, List`1 insertItem)

  28.    at SqlSugar.InsertableProvider`1.Init()

  29.    at SqlSugar.SqlSugarProvider.CreateInsertable[T](T[] insertObjs)

  30.    at SqlSugar.SqlSugarProvider.Insertable[T](T[] insertObjs)

  31.    at SqlSugar.SqlSugarProvider.Insertable[T](T insertObj)

  32.    at SqlSugar.SqlSugarClient.Insertable[T](T insertObj)

  33.    at SqlSugar.SqlSugarScope.Insertable[T](T insertObj)

  34.    at SqlSugar.SimpleClient`1.InsertAsync(T insertObj)

  35.    at HQ.Core.Repostory.ProductModelRepostory.AddProductModel(ProductDetailDto dto) in D:\Projects\sqlsugarCode\sqlsugartest\SqlSugarTest\HQ.Core.Repostory\Repostorys\ProductModelRepostory.cs:line 19

  36.    at HQ.Core.Service.ProductModelService.AddProductModel(ProductDetailDto dto) in D:\Projects\sqlsugarCode\sqlsugartest\SqlSugarTest\HQ.Core.Service\Services\ProductModelService.cs:line 20

  37.    at HQ.Core.Service.ProductService.AddProduct(ProductDetailDto dto) in D:\Projects\sqlsugarCode\sqlsugartest\SqlSugarTest\HQ.Core.Service\Services\ProductService.cs:line 29

  38.    at HQ.Core.Webapi.Controllers.ProductController.AddProduct(RequestParam`1 request) in D:\Projects\sqlsugarCode\sqlsugartest\SqlSugarTest\HQ.Core.Webapi\Controllers\ProductController.cs:line 45

  39.    at lambda_method(Closure , Object )

  40.    at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()

  41.    at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)

  42.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)

  43.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

  44.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)

  45.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)

  46.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()

  47. --- End of stack trace from previous location where exception was thrown ---

  48.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

  49.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)

  50.    at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

  51.    at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

  52.    at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)

  53.    at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)

  54.    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)





热忱回答4