数据库表上的列名无效

时间:2014-12-26 13:38:39

标签: sql indexing

这里有一些帮助。我无法弄清楚为什么会出现这个错误。

列名称无效' Blogs_Id1'。 列名称无效' Blogs_Id1'。 列名称无效' Blogs_Id1'。   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:列名称无效' Blogs_Id1'。 列名称无效' Blogs_Id1'。 列名称无效' Blogs_Id1'。

来源错误:

第84行: 第85行:@ { 第86行:foreach(在Model.Comments中的var注释){...

这是我的模特

 public class Blogs
{
    public  int Id { get; set; }
    [Required, Display(Name = "Title"), MaxLength(100, ErrorMessage = "The {0}  should be 100   characters maximum"), MinLength(10, ErrorMessage = "The {0} is too short!")]
    public string BlogTitle { get; set; }
    [Required, Display(Name ="Post description"),StringLength(500,ErrorMessage ="{0} too large. Should be maximum of 400 characters"),AllowHtml]
    public string Description { get; set; }
    [Required,MinLength(50,ErrorMessage ="The {0} should be a minimum of 50 characters"),AllowHtml]

    public string Body { get; set; }
    public DateTime PostDate { get; set; }
    public string Author { get; set; }
    public string PostImage { get; set; }
    public virtual  ICollection<Comments> Comments { get; set; }
} 

 public class Comments
{
    public int Id { get; set; }
    public string Username { get; set; }
    public DateTime Date { get; set; }
    [MaxLength(400, ErrorMessage = "Too many characters in comments. Please limit it to 400 characters"), Required]
    public string Comment { get; set; }

    public int Blogs_Id { get; set; }
}

这是我的数据库代码

CreateTable(
            "dbo.Blogs",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    BlogTitle = c.String(nullable: false, maxLength: 100),
                    Description = c.String(nullable: false, maxLength: 500),
                    Body = c.String(nullable: false),
                    PostDate = c.DateTime(nullable: false),
                    Author = c.String(),
                    PostImage = c.String(),
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.Comments",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Username = c.String(),
                    Date = c.DateTime(nullable: false),
                    Comment = c.String(nullable: false, maxLength: 400),
                    Blogs_Id = c.Int(),

                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Blogs", t => t.Blogs_Id)
            .Index(t => t.Blogs_Id);

成功创建了博客和评论表。但是,当我想访问我打算显示评论的帖子详细信息时,上面的错误仍然存​​在。

供参考,这里是视图代码(评论部分)

@{
        foreach (var comment in Model.Comments) { 

    <div class="media">

        <div class="media-body">
            <img src="~/images/user.png" width="200" height="200" style="margin: 0 15px 15px 0; float:left" alt="user" title="@comment.Username" />
            <h4 class="media-heading">

                @comment.Username
                <small>@comment.Date.ToLongDateString()</small>
            </h4>
            @comment.Comment
        </div> 
    </div>
    }}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

根据您的Blogs创建字符串,外键的列名应为ID。不是Blogs_ID。

ForeignKey(&#34; dbo.Blogs&#34;,t =&gt; t.Blogs_Id) - &gt; ForeignKey(&#34; dbo.Blogs&#34;,t =&gt; t.Id)