2016-10-15 20 views
5

我正在玩linux上的dotnet核心,我正在嘗試使用連接字符串配置我的DbContext到一個mysql服務器。在DbContextOptions上找不到UseMysql方法

我的DbContext看起來像這樣:

using Microsoft.EntityFrameworkCore; 
using Models.Entities; 

namespace Models { 
    public class SomeContext : DbContext 
    { 
     //alot of dbSets... 
     public DbSet<SomeEntity> SomeDbSet { get; set; } 

     public EsportshubContext(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 
     } 
    } 
} 

我的依賴性看起來像這樣:

"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" 
    }, 

我也試着使用mysql服務器在我Startup.cs下面的代碼

public class Startup 
    { 

     public void ConfigureServices(IServiceCollection services) 
     { 
      var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;"; 
      services.AddDbContext<EsportshubContext>(options => options.UseMysql); //Cannot find UseMysql* 
     } 

我試圖改變我的指令

using Microsoft.EntityFrameworkCore; 

using MySQL.Data.EntityFrameworkCore; 

這是合理的?也許?但是然後所有對DbContext和DbSet的引用都消失了,所以我想這個解決方案是混合的。

回答

11

你需要

using Microsoft.EntityFrameworkCore; 
using MySQL.Data.EntityFrameworkCore.Extensions; 

甲骨文不符合該標準的做法使用依賴注入的時候,所以其所有的有點不同。標準做法是將用於Dependency Injection的擴展方法放入大多數ASP.NET Core應用程序項目中包含的Microsoft.Extensions.DependencyInjection名稱空間中,以便在導入包時該方法自動可用。

+0

你是怎麼知道MySQL.Data.EntityFrameworkCore.Extensions是你必須使用的確切的「使用語句」? – DenLilleMand

+1

嘿,Oracle沒有辦法將它們的代碼放在'Microsoft。*'命名空間:) –

+0

@IvanStoev:它不是任何特定於框架的代碼,只是用於注入/註冊Mysql提供程序的擴展方法,所以就是Microsoft。 Extension.DependencyInjection'完全適用於它,因爲代碼對於在(ASP).NET Core – Tseng