1
votes

I cant solve this error when I add a new row my MVC DB project even though I see people have solved this before with Linq. I am not using Linq but EF. I have a MVC project, VS 2010 and connected to a sql 2008 via designer

1)created internet project 2) right click on models - > add new item-> ado entity data model and select the remote DB.

3)create controller 4) run project and see the screen to create new row. I do this and get this error after I add 2 rows. I pre-create the table and have the PK defined in sql server 2008 as an Int

Server Error in '/' Application. Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF.

I didnt write any code so so I am not sure what else I need to say

[Serializable()] [DataContractAttribute(IsReference=true)] public partial class video : EntityObject { #region Factory Method

    /// <summary>
    /// Create a new video object.
    /// </summary>
    /// <param name="id">Initial value of the ID property.</param>
    public static video Createvideo(global::System.Int32 id)
    {
        video video = new video();
        video.ID = id;
        return video;
    }

    #endregion
    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 ID
    {
        get
        {
            return _ID;
        }
        set
        {
            if (_ID != value)
            {
                OnIDChanging(value);
                ReportPropertyChanging("ID");
                _ID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("ID");
                OnIDChanged();
            }
        }
    }
    private global::System.Int32 _ID;
    partial void OnIDChanging(global::System.Int32 value);
    partial void OnIDChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
3

3 Answers

1
votes

Please access .edmx page of your model and then select the identity column. Open the property window (press F4) and set 'StoreGeneratedPattern' property to 'Identity'. This property determines if the corresponding column in the database will be auto-generated during insert and update.

0
votes

can you sent the script for the table in the DB ? maybe that the identity column (identity increment / identity seed ) is not set on the Primary key

0
votes

Ran into the same issue. I used Flavia's method to fix the problem. Other solutions I tried: 1) setting identity_insert to on 2) refreshing database model

I used asp.net MVC 5 EF (database first). I think the problem was, I was rebuilding the model over and over and somewhere along the line I must have turned off identity for that column.