Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Unable to create a 'DbContext' in MySql - Blazor .NET 9

$
0
0

I'm trying to connect Blazor (.NET 9) to a MySql database (8.4.3). I've read a lot of posts and tried different packages without success.

Here are the packages I've installed:

<ItemGroup><PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" /><PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0"><PrivateAssets>all</PrivateAssets><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets></PackageReference><PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0" /><PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0" /><PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" /></ItemGroup>

Here is the appsettings.json file:

"ConnectionStrings": {"DefaultConnection": "Server=x.x.x.x;Database=DB_Cours;User=UserCours;Password=12345678"},

Here is Program.cs:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");builder.Services.AddDbContext<MySqlDbContext>(options =>    options.UseMySql(connectionString, new MySqlServerVersion(new Version(8,4,3))));builder.Services.AddDatabaseDeveloperPageExceptionFilter();

This is my MySqlDbContext.cs:

public class MySqlDbContext(DbContextOptions<MySqlDbContext> options) : DbContext(options){    public DbSet<Person>? Person { get; set; }    protected override void OnModelCreating(ModelBuilder modelBuilder)    {        base.OnModelCreating(modelBuilder);        modelBuilder.Entity<Person>().HasData(            new Person { Id = 1, Name = "Laurent"},            new Person { Id = 2, Name = "John"},            new Person { Id = 3, Name = "Mike" },            new Person { Id = 4, Name = "Larry" }        );    }}

When I'm doing the migration by taping Add-Migration Init I'm getting this error message:

Unable to create a 'DbContext' of type 'RuntimeType'.

The exception 'Method 'get_LockReleaseBehavior' in type 'Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlHistoryRepository' from assembly 'Pomelo.EntityFrameworkCore.MySql, Version=8.0.2.0, Culture=neutral, PublicKeyToken=2cc498582444921b' does not have an implementation.' was thrown while attempting to create an instance.

As I explained I've tried other packages, downgrade versions, and nothing works.

I appreciate your help


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>