0

I am working through a (simple) Entity Framework example in a Console Application just to learn the basics of Database Migrations in EF. All of the other Tables and Columns are being generated fine but this one Column is being created with angle brackets around it in SQL Server Express ( '[Content]' rather than 'Content').

I can't see any thing in the class declaration

 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; }

    }

the Migration file

 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");
    }
}

or in the generated SQL script (I used the -Verbose flag)

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])

)

which would indicate why this particular column is being generated with the angle brackets included?

Can anyone help?

thisextendsthat
  • 1,266
  • 1
  • 9
  • 26
  • From your code, it looks like that all columns are generated with angle brackets. – dario Dec 31 '14 at 16:30
  • I also don't see where one column is being singled out for special behavior. Looks like you don't know much about SQL and can't even be bothered to read the output you posted in the question. I'm voting to close this crap. – siride Dec 31 '14 at 16:33
  • No, only that one particular column name is - the others are surrounded by the angle brackets in the SQL script but these are removed when the column names are actually generated. That's why I am so stumped..? – thisextendsthat Dec 31 '14 at 16:36
  • 1
    Nitpick, `[ ]` are called square brackets. Angle brackets are `< >` – EkoostikMartin Dec 31 '14 at 16:42

2 Answers2

2

I'm not an expert in EF by any means, but I'm guessing it's adding the square brackets around "Content" because this is a reserved keyword in SQL (looks like it's used with the XML datatype); whenever there's a chance for collision, you use the square bracket syntax in T-SQL to denote identifiers (table/column names).

jbsmith
  • 1,616
  • 13
  • 10
  • I think this is it - the angle brackets are there when I view the table in Design View from the Object Explorer (right click Table -> 'Design') BUT they are omitted when I actually retrieve data from the column in the column titles for the record set. Thanks for your help guys. – thisextendsthat Dec 31 '14 at 16:40
1

Square brackets are a way to quote identifiers in SQL. Because EF has to be able to handle arbitrary names (like "some column name"), it quotes everything to make sure that it produces working code.

Ben Thul
  • 31,080
  • 4
  • 45
  • 68