Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在调用 Db.CodeFirst.InitTables(@"xxx.xxx.models"); 显示System.IO.FileNotFoundException: Could not load file or assembly #1237

Closed
PSC-F opened this issue Apr 25, 2024 · 3 comments

Comments

@PSC-F
Copy link

PSC-F commented Apr 25, 2024

Warning

在Aspire 中引入 SqlSugar 初始化数据库表结构,调用按指定命名空间初始化表时找不到程序集

  • 使用版本
<PackageReference Include="SqlSugarCore" Version="5.1.4.152" />
  • 堆栈
An unhandled exception has occurred while executing the request.
      System.IO.FileNotFoundException: Could not load file or assembly 'ZhangPengFei.IoT.ApiService.Model, Culture=neutral, PublicKeyToken=null'. 系统找不到指定的文件。
      File name: 'ZhangPengFei.IoT.ApiService.Model, Culture=neutral, PublicKeyToken=null'
         at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
         at System.Reflection.Assembly.Load(String assemblyString)
         at SqlSugar.CodeFirstProvider.InitTables(String entitiesNamespace)
         at ZhangPengFei.IoT.ApiService.DataBase.DataBaseManager.BuildWithMySQL() in C:\Users\doudo\RiderProjects\Aspire.IoT\AspireIoT\ZhangPengFei.IoT.ApiService\DataBase\DataBaseManager.cs:line 40
         at ZhangPengFei.IoT.ApiService.EndPoints.GateWayEndPoints.Services.GateWayService..ctor(MySqlDataSource dataSource) in C:\Users\doudo\RiderProjects\Aspire.IoT\AspireIoT\ZhangPengFei.IoT.ApiService\EndPoints\GateWayEndPoints\Services\GateWayService.cs:line 11
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
         at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
         at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
         at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
         at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
         at lambda_method4(Closure, Object, HttpContext)
         at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.Invoke(HttpContext context)
  • 临时解决方案
Db.DbMaintenance.CreateDatabase();
        // todo: bug System.IO.FileNotFoundException: Could not load file or assembly
        // Db.CodeFirst.InitTables(@"ZhangPengFei.IoT.ApiService.Model");
        // todo: fix init tables
        Assembly assembly = Assembly.GetExecutingAssembly();
        var types = assembly.GetTypes().Where(t => t.Namespace == typeof(GateWay).Namespace).ToArray();
        Db.CodeFirst.InitTables(types);
    
@PSC-F PSC-F changed the title bug 在调用 Db.CodeFirst.InitTables(@"xxx.xxx.models"); 显示System.IO.FileNotFoundException: Could not load file or assembly 在调用 Db.CodeFirst.InitTables(@"xxx.xxx.models"); 显示System.IO.FileNotFoundException: Could not load file or assembly Apr 25, 2024
@DotNetNext
Copy link
Owner

不要用这个方法,建议用这种
image

@DotNetNext
Copy link
Owner

image
string重载只是用的微软底层方法,可能不能跨程序集。

@PSC-F
Copy link
Author

PSC-F commented Apr 25, 2024

image string重载只是用的微软底层方法,可能不能跨程序集。

好的,知道了感谢 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants