Home > Software design >  C# Entity Framework Core: unable to resolve service
C# Entity Framework Core: unable to resolve service

Time:02-07

I'm trying to use Entity Framework Core for C# for the first time. 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'.

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)

//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();

CodePudding user response:

if you setup your database properly then try this command "dotnet ef migrations add Initial" for update try "dotnet ef database update"

CodePudding user response:

im using net6 so i dont have a startup.cs

This is the root cause of the problem. From Design-time DbContext Creation documentation topic, even though not stated explicitly, looks like that "minimal API" style code is not supported, since they still need a special method

public static IHostBuilder CreateHostBuilder(string[] args)

in the Program class. Hence most likely you need to move most of your code (between CreateBuilder(args) and .Build()) into a method with the above signature. Or use other methods of creating db context at design time, for instance From a design-time factory and somehow get the settings needed to create the db context. 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?. 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. You can watch EF Core team responses there and eventually see what is their vision on the subject.

CodePudding user response:

<PackageReference Include="EntityFramework" Version="6.4.4" />

This is not Entity Framework core, but the old EF 6. You need to install/replace it with

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />

Or the version of your liking.

Also, the MySql provider you are using is not so great. You might want to switch to Pomelo

  •  Tags:  
  • Related