简体   繁体   English

C# 实体框架核心:无法解析服务

[英]C# Entity Framework Core: unable to resolve service

I'm trying to use Entity Framework Core for C# for the first time.我第一次尝试将 Entity Framework Core 用于 C#。 My project does compile, but my add-migration does not run.我的项目确实编译了,但我的添加迁移没有运行。

I get the error:我得到错误:

Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.TypeMappingSourceDependencies' while attempting to activate 'MySql.EntityFrameworkCore.Storage.Internal.MySQLTypeMappingSource'.尝试激活“MySql.EntityFrameworkCore.Storage.Internal.MySQLTypeMappingSource”时无法解析“Microsoft.EntityFrameworkCore.Storage.TypeMappingSourceDependencies”类型的服务。

My project file:我的项目文件:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
    <PackageReference Include="EntityFramework" Version="6.4.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="MySql.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>
</Project>

My DbContext:我的数据库上下文:

internal class DbModelContext : DbContext
{
    public DbSet<DbUser> Users { get; set; }    
    public DbModelContext(DbContextOptions<DbModelContext> options)
        : base(options) { }
}

My user table:我的用户表:

internal class DbUser
{
    [Key]
    public string? Email { get; set; }
    public string? PasswordHash { get; set; }
    public Guid UserId { get; set; }
}

Here is my Program.cs (im using net6 so i dont have a startup.cs)这是我的 Program.cs(我使用 net6,所以我没有 startup.cs)

//Init DbConnectorClass
DatabaseConnector dbConnector = new();

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

//TODO: move to appsettings.json
var connString = "server=192.168.178.XXX;user=XXX;database=XXX;password=XXX;port=3306";

builder.Services.AddDbContext<DbModelContext>(options => options.UseMySQL(connString));
builder.Services.AddScoped<IAuthenticationHandler>(ah => new AuthenticationHandler(dbConnector));

WebApplication app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

如果您正确设置数据库,请尝试使用此命令“dotnet ef migrations add Initial”进行更新尝试“dotnet ef database update”

"

im using net6 so i dont have a startup.cs我使用net6,所以我没有startup.cs

<\/blockquote>

This is the root cause of the problem.这是问题的根本原因。 From Design-time DbContext Creation<\/a> documentation topic, even though not stated explicitly, looks like that "minimal API" style code is not supported, since they still need a special method设计时 DbContext 创建<\/a>文档主题,即使没有明确说明,看起来也不支持“最小 API”样式代码,因为它们仍然需要特殊方法

public static IHostBuilder CreateHostBuilder(string[] args)<\/code><\/pre>

in the Program<\/code> class.Program<\/code>类中。 Hence most likely you need to move most of your code (between CreateBuilder(args)<\/code> and .Build()<\/code> ) into a method with the above signature.因此,您很可能需要将大部分代码(在CreateBuilder(args)<\/code>和.Build()<\/code>之间)移动到具有上述签名的方法中。 Or use other methods of creating db context at design time, for instance From a design-time factory<\/a> and somehow get the settings needed to create the db context.或者使用其他在设计时创建数据库上下文的方法,例如从设计时工厂<\/a>并以某种方式获取创建数据库上下文所需的设置。 I guess refactoring the startup code would be easier.我想重构启动代码会更容易。

There is a (closed?!) issue on EF Core GitHub issue tracker - #3557: How to access to WebHostBuilder in Design-time DbContext Creation with top-level statements?<\/a> EF Core GitHub 问题跟踪器上有一个(已关闭?!)问题 - #3557:如何使用顶级语句在设计时 DbContext 创建中访问 WebHostBuilder?<\/a> . . I posted a comment there asking for updating the documentation and providing an example for "minimal API" projects, and included a link to your question.我在那里发表了一条评论,要求更新文档并提供“最小 API”项目的示例,并包含指向您问题的链接。 You can watch EF Core team responses there and eventually see what is their vision on the subject.您可以在那里观看 EF Core 团队的回复,并最终了解他们对该主题的看法。

"

<PackageReference Include="EntityFramework" Version="6.4.4" />
public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddEntityFrameworkMySQL();
        new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
            .TryAddCoreServices();
    }
}

You can add one another class in startup.cs file, in order to get this fixed.您可以在 startup.cs 文件中添加另一个 class 以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM