C# &引用;当identity_insert设置为OFF时,无法在表{Tags}中为identity列插入显式值;

C# &引用;当identity_insert设置为OFF时,无法在表{Tags}中为identity列插入显式值;,c#,sql-server,entity-framework,model-view-controller,C#,Sql Server,Entity Framework,Model View Controller,我在尝试将标记类的新实例添加到数据库时一直遇到此错误。这个错误是针对我的Tags DB表的,所以我猜我的代码试图以某种方式将ID值传递给列,该列已经是标识并自动递增 下面是PostController中CreatePost方法的相关信息副本: public ActionResult CreatePost(Post postFromForm, string tags) { postFromForm.CreatedOn = DateTime.Now; pos

我在尝试将标记类的新实例添加到数据库时一直遇到此错误。这个错误是针对我的Tags DB表的,所以我猜我的代码试图以某种方式将ID值传递给列,该列已经是标识并自动递增

下面是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();
        }
public ActionResult CreatePost(Post postFromForm,字符串标记)
{
postFromForm.CreatedOn=DateTime.Now;
Tags=newlist();
string[]tagList=tags.Split(“”);
foreach(标记列表中的var标记)
{
标签NewTag=新标签();
NewTag.Name=tag;
postFromForm.Tags.Add(NewTag);
}
使用(var dbContext=new blogEntities())
{
dbContext.Posts.Add(postFromForm);
dbContext.SaveChanges();
}
以及将参数传递给方法的表单字段:

 @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" />
@Html.TextBox(“title”,“”,new{@class=“form control”,@placeholder=“title”})
@TextArea(“body”,new{@class=“BodyEdit表单控件”,“占位符=”开始在这里键入…”)
@TextBox(“tags”,“”,new{@class=“form control”,@placeholder=“tags”})
为了更好的衡量,请看一下我的课程:

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; }
}
公共部分类标记
{
公共标签()
{
this.Posts=newhashset();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection Posts{get;set;}
}
公共部分类职位
{
公职人员职位()
{
this.Comments=newhashset();
this.Tags=newhashset();
}
公共字符串标题{get;set;}
公共字符串体{get;set;}
public System.DateTime CreatedOn{get;set;}
公共int Id{get;set;}
公共可为null的EditedOn{get;set;}
通过{get;set;}创建的公共字符串
公共虚拟ICollection注释{get;set;}
公共虚拟ICollection标记{get;set;}
}
如果有任何见解,我将不胜感激——我整个下午都在忙着写这篇文章。如果可能的话,有人知道仅仅改变IDENTITY_INSERT的值(我尝试过,但没有成功)是否是解决问题的原因而不是症状的方法吗?

可能的答案

更新您的edmx文件以反映您在数据库中所做的任何更改。如果数据库自动分配该值,您应该在该属性下的设计器文件中看到“IsDbGenerated=true”属性。如果该属性不存在,您可以手动添加它


确保您的EF模型与您的数据库相匹配……特别是自动增量字段。如果这是DB first,请刷新您的模型。如果是code first,是否有流畅的代码?Edmx已经过时,我觉得非常愚蠢,但感谢您的提示。