依赖包
Pomelo.EntityFrameworkCore.MySql 2.0.0
准备工作
在Models下创建一个User.cs文件
using …
Pomelo.EntityFrameworkCore.MySql 2.0.0
在Models下创建一个User.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MvcMovie.Models
{
public class User
{
public int UserId { set; get; }
public string Name { set; get; }
}
}
在工程下面创建一个Data目录,然后在Data目录下创建一个DataContext.cs文件
using Microsoft.EntityFrameworkCore;
using MvcMovie.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MvcMovie.Data
{
public class DBContext : DbContext
{
public DbSetUsers { set; get; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseMySql("server=127.0.0.1;database=aspdotnettest;uid=root;pwd=root;charset='utf8';SslMode=None");
}
}
随便在哪个控制器下面使用以下代码
using (var context = new DBContext())
{
context.Database.EnsureCreated();
var user = new User { Name = "愤怒的TryCatch" };
context.Add(user);
context.SaveChanges();
}
然后从浏览器访问这个控制器,最后查看数据库的变化。
使用“MySql.Data.EntityFrameworkCore ”遇到以下错误
Method 'Clone' in type 'MySQL.Data.EntityFrameworkCore.Infraestructure.Internal.MySQLOptionsExtension' from assembly
经过查找,在这里找到了答案:https://blogs.msdn.microsoft.com/dotnet/2017/05/12/announcing-
ef-core-2-0-preview-1/
找到这段话: If you are using a third party database provider, then check to see if
they have released an update that depends on 2.0.0-preview1-final. If they
have, then just upgrade to the new version. If not, then you will not be able
to upgrade since version 2.0 contains several breaking changes and 1.*
providers are not expected to work with it.
所以答案是:
坐等MySQL.Data.EntityFrameworkCore更新吧,或者使用Pomelo.EntityFrameworkCore.MySql
2.0.0预发行版。
如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。
本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。