【.NET_EFCore中常见错误汇总】

2023-05-16

记录一下在配置分层项目中所遇到的问题:

问题一:

        启动项目没有引用:Your startup project '8.1-MVC ' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

 解决方法:将AdvantService该项目设置为启动项

问题二:

        Your target project '6.4-EFCore多层' doesn't match your migrations assembly '6.4-EFCoreBooks'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("6.4-EFCore多层")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.  

 解决方法:注入EFCore时,设置包含基于代码的迁移的程序集

//注入EFCore
builder.Services.AddDbContextPool<DBContext>(opt => {
    string? conStr = builder.Configuration.GetSection("conStr").Value;
    opt.UseSqlServer(conStr, c => c.MigrationsAssembly("6.4-EFCore多层"));
});

  

问题三: 

        Your startup project '6.4-EFCore多层' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

解决方法:引用Nuget包:Install-Package Microsoft.EntityFrameworkCore.Design -Version 7.0.0-preview.7.22376.2  

问题四:

        A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 证书链是由不受信任的颁发机构颁发的。)  

问题五:

     Exception has bee  thrown by thetargetofaninvocation

     System.Reflection.TargetInvocationException:Exception has been thrown by the target of an invocation.-System.ArgumentNullException:Value cannot be null(Parameter'connectionString')atMicrosoft.EntityFrameworkCore.Utilities.Check.NotEmpty (Stringvalue,StringparameterName)at_6._4_EFCoreBooks.Migration.DBContextDesignFactory.

CreateDbContext(String[]args)in  

 解决办法:通常是由于数据库连接字符串有误引起的,检查连接字符串

问题六:

        A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 证书链是由不受信任的颁发机构颁发的。)  

解决方法: 在数据库连接字符串后加Encrypt=False;

问题七:

        Add-Migration : 无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

解决方法:项目安装Install-Package Microsoft.EntityFrameworkCore.Tools -Version 7.0.0-preview.7.22376.2

 

以上是我在配置EFCore时出现的问题,希望能给大家提供帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【.NET_EFCore中常见错误汇总】 的相关文章

随机推荐