【发布时间】: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 => 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