8

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();
2
  • Can you include a copy of your ConfigureServices in Startup.cs too please? That's where the initial dependency injection will occur and is relevant for this. Commented Feb 6, 2022 at 21:07
  • @JayGould , i did thanks for the suggestion Commented Feb 6, 2022 at 21:18

5 Answers 5

6
<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

Sign up to request clarification or add additional context in comments.

4 Comments

that was the problem, kind of confusing to be honest, this versioning got me good, I also switched to pomelo, as you recommended
@Elias tbh you should probably follow a good tutorial (e.g. Microsoft) when starting on something like this.
Switching to Pomelo fixed my problem. Refactoring the top-level statements to the older style did nothing to fix the error (but I'm too lazy to undo that work).
Switching to Pomelo is the best way to go. I migrated from .Net core to .Net 6 so I had to alter some old migrations a bit and it worked
5
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.

1 Comment

Source and explanation on why this works: bugs.mysql.com/bug.php?id=106592
2

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.

Comments

0

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

Comments

0

solution from https://bugs.mysql.com/bug.php?id=106592

Added this class to resolve your problem

public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddEntityFrameworkMySQL();
        new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
            .TryAddCoreServices();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.