Se0ng11
Se0ng11

Reputation: 2333

EF insert not follow db auto increment(DB First)

.EDMX

<Property Name="uid" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />

EF had set the StoreGeneratedPattern = "Identity", but why I still cannot save into db? Debug show that the value are always 0, guess is quite correct as server not return the value to the web before inserting, but model validate always false, and I had go through C#, entity framework, auto increment and Autonumber with Entity Framework but no luck, what am I doing wrong? Any idea?

Update 1:

Added model and meta data picture, forgot to mention that Im using EF DB first to generate the class

ASP.NET MVC View

<div class="editor-field">
    @Html.HiddenFor(model => model.uid)
</div>

ASP.NET MVC Controller

[HttpPost, ValidateJsonAntiForgeryToken]
        public JsonResult Create(BasicUserInfo basicuserinfo)
        {
            if (ModelState.IsValid)
            {
                db.BasicUserInfoes.Add(basicuserinfo);
                db.SaveChanges();
            }
            else 
            {
                return Json(new { valid = "N", err = CaptureError() });
            }

            return Json(new { valid = "Y", route = Url.Action("Index") });
        }

Script

            var self = this;
            var data = "";
            var _model = new model();
            var data = JSON.stringify({ "BasicUserInfo": $(e.currentTarget).serializeObject() });
                var _Create = new Create();
                _Create.fetch({
                    type: 'POST',
                    headers: headers,
                    data: data,
                    contentType: "application/json; charset=UTF-8",
                    success: function (data, textStatus, jqXHR) {
                        if (textStatus.valid == "Y") {
                            _model.AppendAddSuccess(textStatus.route);
                        } else {
                            _model.AppendFailed(textStatus.err);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        console.log(textStatus.statusText);
                    }
                });

SQL Server 2008 enter image description here

Error

enter image description here

Modal
enter image description here

MetaData

enter image description here

Upvotes: 1

Views: 1360

Answers (1)

ElliotSchmelliot
ElliotSchmelliot

Reputation: 8352

It looks like Entity Framework is not recognizing the property as a table field. When creating an entity, you also shouldn't need to specify an html helper for its primary key if it is set to IsIdentity = true. If EF recognizes the entity, it will automatically generate a primary key for this field. Therefore you only need html helpers for its other fields.

Make sure your model property is the same name as your database column. In this case, your db column should be titled uid it looks like. You can specify the target column in your model using the Column attribute if you don't want to change your db tables.

Upvotes: 1

Related Questions