【发布时间】:2014-12-31 16:22:54
【问题描述】:
我正在控制台应用程序中处理(简单)Entity Framework example,只是为了了解 EF 中数据库迁移的基础知识。所有其他表和列都生成良好,但这一列是在 SQL Server Express 中用尖括号创建的(“[Content]”而不是“Content”)。
我在类声明中看不到任何东西
public class Post
{
public int PostID { get; set; }
[MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public int BlogID { get; set; }
public Blog Blog { get; set; }
}
迁移文件
public partial class AddPostClass : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Posts",
c => new
{
PostID = c.Int(nullable: false, identity: true),
Title = c.String(maxLength: 200),
Content = c.String(),
BlogID = c.Int(nullable: false),
})
.PrimaryKey(t => t.PostID)
.ForeignKey("dbo.Blogs", t => t.BlogID, cascadeDelete: true)
.Index(t => t.BlogID)
.Index(p => p.Title, unique: true);
AddColumn("dbo.Blogs", "rating", c => c.Int(nullable: false));
}
public override void Down()
{
DropForeignKey("dbo.Posts", "BlogID", "dbo.Blogs");
DropIndex("dbo.Posts", new[] { "Title" });
DropIndex("dbo.Posts", new[] { "BlogID" });
DropColumn("dbo.Blogs", "rating");
DropTable("dbo.Posts");
}
}
或者在生成的 SQL 脚本中(我使用了 -Verbose 标志)
CREATE TABLE [dbo].[Posts] (
[PostID] [int] NOT NULL IDENTITY,
[Title] [nvarchar](200),
[Content] [nvarchar](max),
[BlogID] [int] NOT NULL,
CONSTRAINT [PK_dbo.Posts] PRIMARY KEY ([PostID])
)
这将表明为什么生成此特定列时包含尖括号?
谁能帮忙?
【问题讨论】:
-
从您的代码看来,所有列都是用尖括号生成的。
-
我也看不到哪一列因特殊行为而被挑出。看起来您对 SQL 了解不多,甚至懒得阅读您在问题中发布的输出。我投票结束这个废话。
-
不,只有一个特定的列名是 - 其他的在 SQL 脚本中用尖括号括起来,但在实际生成列名时会删除这些列名。这就是为什么我这么难过..?
-
Nitpick,
[ ]被称为方括号。尖括号是< >
标签: c# sql-server entity-framework visual-studio tsql