【问题标题】:EF Code First Migration doesn't compileEF Code First Migration 无法编译
【发布时间】:2016-10-31 12:25:20
【问题描述】:

我使用 EF Code First 来生成我的数据库架构(EF 6、.NET 4.5.2)。我有两个要插入的类:

public class MatrixFile
{
    [Key]
    public int Id { get; set; }

    public string FilePath { get; set; }

    public virtual List<MatrixData> Data { get; set; }
}

public class MatrixData
{
    [Key]
    public int Id { get; set; }

    public int OdPairKey { get; set; }

    public double Value { get; set; }
}

这是我的 DbContext

public class MyDataContext : DbContext
{
    public virtual DbSet<MatrixFile> MatrixFiles { get; set; }
    public virtual DbSet<MatrixData> MatrixData { get; set; }
}

我尝试通过在包管理器控制台中输入以下命令,在 EF 中生成我的 DB Schema:

Enable-Migrations
Add-Migration GenerateDatabase

这是 EF 为我生成的迁移:

public partial class GenerateDatabase : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.MatrixDatas",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    OdPairKey = c.Int(nullable: false),
                    Value = c.Double(nullable: false),
                    MatrixFile_Id = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.MatrixFiles", t => t.MatrixFile_Id)
            .Index(t => t.MatrixFile_Id, name: ""IX_MatrixData_MatrixFile_Id"");

        CreateTable(
            "dbo.MatrixFiles",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    FilePath = c.String(),
                })
            .PrimaryKey(t => t.Id);

    }

    public override void Down()
    {
        DropForeignKey("dbo.MatrixDatas", "MatrixFile_Id", "dbo.MatrixFiles");
        DropIndex("dbo.MatrixDatas", ""IX_MatrixData_MatrixFile_Id"");
        DropTable("dbo.MatrixFiles");
        DropTable("dbo.MatrixDatas");
    }
}

.Index(t =&gt; t.MatrixFile_Id, name: ""IX_MatrixData_MatrixFile_Id""); 行存在编译错误,因为 IX_MatrixData_MatrixFile_Id 周围有双引号。为什么会这样?如何在不手动破解迁移的情况下生成正确的迁移代码?

【问题讨论】:

  • 为什么那里有一个双引号?只需删除它,它就会工作
  • 嗨@Stormhashe。您的评论基本上是在改写我的问题,但拼写和语法要差得多。我知道我可以通过删除多余的引号来解决它。但是,您知道 EF 生成代码的原因吗?
  • 你在使用 MSSQL 吗?
  • @MichaelCoxon 是和否。我正在使用默认的 LocalDB 连接,它使用 System.Data.SqlClient 提供程序。
  • 我刚刚浏览了 EF6 代码。看起来您正在某处手动设置索引名称..您可以发布您的 OnModelCreating 覆盖或其他任何您正在设置索引名称的地方。

标签: c# entity-framework ef-code-first entity-framework-6 entity-framework-migrations


【解决方案1】:

为了进一步解释我对 OP 的评论,EF 在 Migrations Scaffolder 中有这个...

// System.Data.Entity.Migrations.Design.CSharpMigrationCodeGenerator
/// <summary>
/// Quotes an identifier using appropriate escaping to allow it to be stored in a string.
/// </summary>
/// <param name="identifier"> The identifier to be quoted. </param>
/// <returns> The quoted identifier. </returns>
protected virtual string Quote(string identifier)
{
    return "\"" + identifier + "\"";
}

...由表脚手架和索引脚手架调用。这会将代码生成为要写入 .cs 文件的字符串...

// System.Data.Entity.Migrations.Design.CSharpMigrationCodeGenerator
private void WriteIndexParameters(CreateIndexOperation createIndexOperation, IndentedTextWriter writer)
{
    if (createIndexOperation.IsUnique)
    {
        writer.Write(", unique: true");
    }
    if (createIndexOperation.IsClustered)
    {
        writer.Write(", clustered: true");
    }
    if (!createIndexOperation.HasDefaultName)
    {
        writer.Write(", name: ");
        writer.Write(this.Quote(createIndexOperation.Name));
    }
}

问题是,如果idenitifier"\"IX_MYIndex\"",您将在代码中得到引号未转义。您真正需要的 - 如果您出于任何原因需要引号是 "\\\"IX_MYIndex\\\"",以便转义序列在 C# 中有效。

【讨论】:

    【解决方案2】:

    你在钱上是对的@MichaelCoxon。我的意图是最终将它与 SQLite 一起使用,并且我安装了 SQLite.CodeFirst 包,因此我可以使用它来初始化数据库。我在 DataContext 中有以下代码:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<EngineDataContext>(modelBuilder);
            Database.SetInitializer(sqliteConnectionInitializer);
        }
    

    这似乎生成了恶意代码。删除它/注释掉它可以解决问题。我不知道OnModelCreating会影响EF生成的代码!

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多