Reputation: 4560
I am playing around with the dotnet core on linux, and i am trying to configure my DbContext with a connection string to a mysql Server.
my DbContext looks like so:
using Microsoft.EntityFrameworkCore;
using Models.Entities;
namespace Models {
public class SomeContext : DbContext
{
//alot of dbSets...
public DbSet<SomeEntity> SomeDbSet { get; set; }
public SomeContext(DbContextOptions<SomeContext> options) : base(options) {
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMysql //Cannot find this method
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//using modelBuilder to map some relationships
}
}
}
My dependencies looks like so:
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.AspNetCore.Mvc":"1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.1",
"MySql.Data.Core": "7.0.4-ir-191",
"MySql.Data.EntityFrameworkCore": "7.0.4-ir-191"
},
I also tried to use the mysql server in my Startup.cs in the following code
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;";
services.AddDbContext<SomeContext>(options => options.UseMysql); //Cannot find UseMysql*
}
I tried to change my directive from
using Microsoft.EntityFrameworkCore;
to
using MySQL.Data.EntityFrameworkCore;
Which makes sense? maybe? but then all of the references to DbContext and DbSet is gone, so i suppose the solution is a mix of sorts??
Upvotes: 21
Views: 62055
Reputation: 4928
Use the below code if you are referencing the package Pomelo.EntityFrameworkCore.MySql
services.AddDbContextPool<AppDBContext>(options =>
{
var connetionString = Configuration.GetConnectionString("DefaultConnection");
options.UseMySql(connetionString, ServerVersion.AutoDetect(connetionString));
});
Upvotes: 22
Reputation: 27852
Future readers.
If you are using "MySql.Data.EntityFrameworkCore":
I have this: (note, the cAsE of any word/phrase containing "MySql".)
In my top-layer (.exe usually) where I do the DI configuration.
csproj (top layer)
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.19" />
cs file with DI
using Microsoft.EntityFrameworkCore;
using MySql.Data.EntityFrameworkCore;
///where servColl is IServiceCollection
servColl.AddDbContext<MyCoolDbContext>(options => options.UseMySQL("server=localhost;database=library;user=mysqlschema;password=mypassword"));
NOTE above the CASE of UseMySQL. Why the namespace is "MySql" and the "use" is "UseMySQL" (??)...... this inconsistency (with the cAsE) is worth noting if you're hitting your head against the screen. :)
and in the "lower layer" (my "data layer") where I code to EntityFramework Core (but not any specific concrete)
csproj (data layer) (note the 2.1 versions.......)
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.*" />
======================================
IF you are using Pomelo.EntityFrameworkCore.MySql. (Which I personally think is a better option.)
In my top-layer (.exe usually) where I do the DI configuration.
csproj (top layer)
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.*" />
cs file with DI
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
///where servColl is IServiceCollection
servColl.AddDbContext<MyCoolDbContext>(options => options.UseMySql("server=localhost;database=library;user=mysqlschema;password=mypassword"));
NOTE above the CASE of UseMySql. This is consistent. So bonus points for Pomelo.EntityFrameworkCore.MySql for consistency between the "using (namepsace)" and the DI registration "UseMySql".
and in the "lower layer" (my "data layer") where I code to EntityFramework Core (but not any specific concrete)
csproj (data layer)
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.*" />
Upvotes: 9
Reputation: 5041
If you are not using MySQL.Data.EntityFrameworkCore
package. You could be using the Pomelo.EntityFrameworkCore.MySql
package for MySql on EntityFramework Core.
If so, add these lines:
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore;
Upvotes: 4
Reputation: 64150
You need
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
Oracle is not complying to the standard practices when using Dependency Injection, so its all a bit different. The standard practice is to put the extension methods for Depedency Injection into Microsoft.Extensions.DependencyInjection
namespace, which is included in most ASP.NET Core app projects so the method becomes automatically available when a package is imported.
Upvotes: 28