简体   繁体   English

使用实体框架核心访问MySql

[英]Access MySql with Entity Framework Core

I try to connect my MySQL database to the test code of the ASP.NET Core (v2.1) application. 我尝试将MySQL数据库连接到ASP.NET Core(v2.1)应用程序的测试代码。 I created a sample application and added the MySql.Data.EntityFrameworkCore nuget package... 我创建了一个示例应用程序,并添加了MySql.Data.EntityFrameworkCore nuget包...

Bellow is my code: 贝娄是我的代码:

// Context
public class EntriesContext: DbContext
{
    public EntriesContext(DbContextOptions<EntriesContext> options) : base(options) { }
    public DbSet<Entry> Entries { get; set; }
}

// Controller
[ApiController, Route("api/[controller]")]
public class EntriesController : ControllerBase
{
    private readonly EntriesContext _context;

    public EntriesController(EntriesContext context) {
        _context = context;
        _context.Database.EnsureCreated();

        if (_context.Entries.Count() == 0) {
            _context.Entries.Add(new Entry {
                From = "default@from.com", To = "default@to.com",
                Message = "default message", Site = "default site"
            });
            _context.SaveChanges();
        }
    }
    [HttpGet]
    public ActionResult<List<Entry>> GetAll() {
        return _context.Entries.ToList();
    }

// Startup
public void ConfigureServices(IServiceCollection services) {
  services.AddDbContext<EntriesContext>(
    opt => opt.UseMySQL("server=localhost;database=mydb;user=myuser;password=mypass"));
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

the Model is like this: 该模型是这样的:

public class Entry
{
    public Entry() {
        this.Date = DateTime.UtcNow
           .Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
           .TotalMilliseconds;
    }
    public Entry(long id, string site, string from, string to, string message) : this() {
        this.Id = id;
        this.Site = site;
        this.From = from;
        this.To = to;
        this.Message = message;
    }

When I launch the application, I get the following: 启动应用程序时,我得到以下信息:

在此处输入图片说明

System.MissingMethodException
  HResult=0x80131513
  Message=Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory..ctor(Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`1<Command>, Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)'.
  Source=MySql.Data.EntityFrameworkCore
  StackTrace:
   at MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLCommandBuilderFactory..ctor(IDiagnosticsLogger`1 logger, IRelationalTypeMapper typeMapper)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at [...]
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
   at LoggerApi.Controllers.EntriesController..ctor(EntriesContext context) in C:\MyProjects\WebApplication1\WebApplication1\Controllers\EntriesController.cs:line 19
   at [...]
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__13.MoveNext()

could someone suggest where is the problem here? 有人可以建议这里的问题出在哪里吗?

Try add package https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/ 尝试添加软件包https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/

Version 2.1.2 版本2.1.2

I worked for me 我为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM