无法在 asp.net core EntityFrameworkCore.core 中创建迁移
Posted
技术标签:
【中文标题】无法在 asp.net core EntityFrameworkCore.core 中创建迁移【英文标题】:Can't create a migration in asp.net core EntityFrameworkCore.core 【发布时间】:2021-10-14 20:31:02 【问题描述】:来自程序集“mysql.Data.EntityFrameworkCore, Version=8.0.22.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d”的“MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory”类型中的方法“Create”没有实现. 它给了我这个错误
namespace DBStuff
public class MessageDBContext : DbContext
public DbSet<Message> Messages get; set;
public MessageDBContext(DbContextOptions<MessageDBContext> options)
: base(options)
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Message>().ToTable("Messages");
modelBuilder.Entity<Message>().HasKey(u => u.ID).HasName("PK_Messages");
modelBuilder.Entity<Message>().HasIndex(p => p.Name).IsUnique().HasDatabaseName("Idx_Name");
modelBuilder.Entity<Message>().Property(ug => ug.ID).HasColumnType("int").IsRequired();
modelBuilder.Entity<Message>().Property(ug => ug.Name).HasColumnType("nvarchar(50)").IsRequired();
modelBuilder.Entity<Message>().Property(ug => ug.Email).HasColumnType("nvarchar(50)").IsRequired(false);
modelBuilder.Entity<Message>().Property(ug => ug.TelephoneNumber).HasColumnType("int").IsRequired();
modelBuilder.Entity<Message>().Property(ug => ug._message).HasColumnType("nvarchar(200)").IsRequired();
消息类:
namespace DBStuff
public class Message
[Key]
public int ID get; set;
[Required]
[MaxLength(50)]
public string Name get; set;
[MaxLength(25)]
public string Email get; set;
[Required]
[MaxLength(15)]
public int TelephoneNumber get; set;
[Required]
[MaxLength(150)]
public string _message get; set;
启动.CS
namespace Somafix
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public IConfiguration Configuration get;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
services.AddControllersWithViews();
//services.AddEntityFrameworkMySQL();
services.AddDbContext<MessageDBContext>(options => options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
endpoints.MapControllerRoute(
name: "default",
pattern: "controller=Home/action=Index/id?");
);
HomeController.cs
namespace Somafix.Controllers
public class HomeController : Controller
private readonly ILogger<HomeController> _logger;
private readonly MessageDBContext _context;
public HomeController(ILogger<HomeController> logger, MessageDBContext context)
_logger = logger;
_context = context;
public IActionResult Index()
return View();
public IActionResult Catalog()
return View();
public IActionResult AboutUs()
return View();
//[HttpGet]
//public IList<Message> Get()
//
// return (this._context.Messages.ToList());
//
public IActionResult ContactUs()
return View();
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
return View(new ErrorViewModel RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier );
【问题讨论】:
也许你应该试试Pomelo.EntityFrameworkCore.MySql。 Oracle 的提供程序可能已过时且不稳定。 您的目标项目“Somafix”与您的迁移程序集“DBStuff”不匹配。更改您的目标项目或更改您的迁移程序集。使用 DbContextOptionsBuilder 更改您的迁移程序集。例如。 options.UseSqlServer(connection, b => b.MigrationsAssembly("Somafix"))。默认情况下,迁移程序集是包含 DbContext 的程序集。通过使用包管理器控制台的默认项目下拉列表,或从包含迁移项目的目录中执行“dotnet ef”,将您的目标项目更改为迁移项目。 现在这个错误啊 我的数据库是空的 已解决 aaa :D 是的 【参考方案1】:已经解决了!
所以我更改了 Nuget 包。我使用的是 mysql.data.entityframeworkcore,现在我使用的是 pomelo.entityframeworkcore.mysql。 我在启动课上写了这个
services.AddDbContext(options => options.UseMySql(connectionString,ServerVersion.AutoDetect(connectionString), b=> b.MigrationsAssembly("Somafix"))); 其中Somafix是我的asp mvc项目,启动类所在的地方
【讨论】:
以上是关于无法在 asp.net core EntityFrameworkCore.core 中创建迁移的主要内容,如果未能解决你的问题,请参考以下文章