简体   繁体   English

“当IDENTITY_INSERT设置为OFF时,无法为表{Tags}中的标识列插入显式值。”

[英]“Cannot insert explicit value for identity column in table {Tags} when IDENTITY_INSERT is set to OFF.”

I keep getting this error when trying to add new instances of my Tag class to the database. 尝试将Tag类的新实例添加到数据库时,我一直收到此错误。 The error is specific to my Tags DB table, so I'm guessing my code is somehow trying to pass an ID value to the column which is already the identity and auto-incrementing. 该错误特定于我的标签数据库表,因此我猜我的代码在某种程度上试图将ID值传递给已经是标识和自动递增的列。

Seen below is a copy of the relevant information from the CreatePost method in the PostController: 下面显示的是PostController中CreatePost方法的相关信息的副本:

public ActionResult CreatePost(Post postFromForm, string tags)
    {
        postFromForm.CreatedOn = DateTime.Now;

        postFromForm.Tags = new List<Tag>();

        string[] tagList = tags.Split(' ');
        foreach (var tag in tagList)
        {
            Tag NewTag = new Tag();
            NewTag.Name = tag;
            postFromForm.Tags.Add(NewTag);
        }

        using (var dbContext = new blogEntities())
        {
            dbContext.Posts.Add(postFromForm);
            dbContext.SaveChanges();
        }

And a look at the form fields which pass parameters into the method: 看一下将参数传递给方法的表单域:

 @Html.TextBox("title", "", new { @class = "form-control", @placeholder = "Title" })
 @Html.TextArea("body", "", new { @class = "BodyEdit form-control", @placeholder = "Start typing here..." })
 @Html.TextBox("tags", "", new { @class="form-control", @placeholder="Tags" })

 <input type="hidden" name="createdby" value="@Session["username"]" />    
 <input type="submit" class="btn btn-primary pull-right" value="Post" />

And for good measure, a look at my classes: 很好地看一下我的课:

public partial class Tag
{
    public Tag()
    {
        this.Posts = new HashSet<Post>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}


public partial class Post
{
    public Post()
    {
        this.Comments = new HashSet<Comment>();
        this.Tags = new HashSet<Tag>();
    }

    public string Title { get; set; }
    public string Body { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public int Id { get; set; }
    public Nullable<System.DateTime> EditedOn { get; set; }
    public string CreatedBy { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

Any insights would be much appreciated - I've been banging my head against the wall with this all afternoon. 任何见解将不胜感激-整个下午我一直都在撞墙。 And if possible does anybody know if simply changing the value of IDENTITY_INSERT (which I tried, didn't work) is a solution for the cause rather than the symptoms? 并且,如果可能的话,有人知道改变IDENTITY_INSERT的值(我尝试过,没有用)是否是针对原因而非症状的解决方案?

Possible answer 可能的答案

Update your edmx file to reflect any changes you may have made in the database. 更新您的edmx文件,以反映您可能在数据库中所做的任何更改。 If the database automatically assigns the value, you should see the "IsDbGenerated=true" attribute in your designer file under that property. 如果数据库自动分配该值,则应该在设计器文件中的该属性下看到“ IsDbGenerated = true”属性。 If it's not there, you can add it manually. 如果不存在,可以手动添加。

Entity Framework error: Cannot insert explicit value for identity column in table 实体框架错误:无法为表中的标识列插入显式值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 {“当IDENTITY_INSERT设置为OFF时,无法在表&#39;Cantact&#39;中为标识列插入显式值。”} - {“Cannot insert explicit value for identity column in table 'Cantact' when IDENTITY_INSERT is set to OFF.”} “当IDENTITY_INSERT设置为OFF时,无法为表&#39;Movies&#39;中的标识列插入显式值。” - “Cannot insert explicit value for identity column in table 'Movies' when IDENTITY_INSERT is set to OFF.” 当IDENTITY_INSERT设置为OFF时,获取“无法在表&#39;OrderPromo&#39;中为标识列插入显式值”。 在桌子上没有身份 - Getting 'Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.' on table with NO Identity 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 当 IDENTITY_INSERT 设置为 OFF 时,无法在“DentalProcedures”中插入标识列的显式值。 EF代码优先 - Cannot insert explicit value for identity column in 'DentalProcedures' when IDENTITY_INSERT is set to OFF. EF code first 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表&#39;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF EF Core给我错误当IDENTITY_INSERT设置为OFF时,无法为表“标签”中的标识列插入显式值 - EF Core giving me error Cannot insert explicit value for identity column in table 'Tags' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表&#39;ActivityInstances中的标识列插入显式值 - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM