使用实体框架核心访问mysql

vwhgwdsa  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(390)

我尝试将mysql数据库连接到asp.net core(v2.1)应用程序的测试代码。我创建了一个示例应用程序并添加了 MySql.Data.EntityFrameworkCore nuget包。。。
下面是我的代码:

// 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);
}

模型如下:

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;
    }

当我启动应用程序时,我得到以下信息:

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()

有人能告诉我哪里出了问题吗?

dzjeubhm

dzjeubhm1#

尝试添加包https://www.nuget.org/packages/pomelo.entityframeworkcore.mysql/
版本2.1.2
我为我工作

相关问题