我正在使用 C#、.NET 4.5、EF6 Code First、MySQL 构建应用程序。
当我添加迁移时,生成的代码格式错误。 “using”语句位于命名空间 block 内。这是一个例子:
错误:(由 EF 生成)
namespace RevoHost.BL.Entities.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class Permissions : DbMigration
{
public override void Up()
{
CreateTable(
// etc...
我预计:
using System;
using System.Data.Entity.Migrations;
namespace RevoHost.BL.Entities.Migrations
{
public partial class Permissions : DbMigration
{
public override void Up()
{
CreateTable(
// etc...
手动修复很容易,但仍然很烦人。我进行了广泛的搜索以寻找解决方案,但到目前为止我无法想出一个解决方案。
我的 DbContext 类上有此属性:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
我像这样配置了迁移:
namespace RevoHost.BL.Entities.Migrations
{
internal sealed class Configuration : DbMigrationsConfiguration<RevoHost.BL.Entities.RevoContext>
{
public Configuration()
{
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();
AutomaticMigrationsEnabled = false;
}
最佳答案
我知道发生了什么事。事实上,正如 Thewads 在他的评论中提到的,代码本身并没有格式错误,而且完全合法。因为这是我的第一个在 MySQL 而不是 SQL Server 上运行的 EF6 项目,所以我立即认为 MySQL 是罪魁祸首,但显然事实并非如此。
我查看了另一个首先使用 EF6 代码进行迁移的项目。该代码完全相同。接下来我检查了从编译器得到的确切错误。
Error 1 The type or namespace name 'CodeDom' does not exist in the namespace 'RevoHost.BL.Entities.System' (are you missing an assembly reference?)
(还有另外 9 个类似的)
我在名为 RevoHost.BL.Entities.System 的命名空间中定义了一个实体。 这会导致命名冲突;编译器先在 RevoHost.BL.Entities 中查找 System 命名空间,然后再找到全局 System 命名空间。
所以,简而言之,答案是,确保您没有定义与系统命名空间冲突的命名空间。
关于c# - 在 MySQL 数据库上首先使用 EF 代码进行格式错误的迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218952/