c# - Insert record to a table using linq query that contains auto increment ID -
i'm having issue insert record table contains auto increment id ,
i'm going insert values table using entity frameworks.
this table creation query
use [db_name] go set ansi_nulls on go set quoted_identifier on go create table [dbo].[table_name]( [discussion_id] [int] identity(1,1) not null, [discussion_name] [nvarchar](50) null, [discription] [nvarchar](255) null, constraint [pk_table_name] primary key clustered ( [discussion_id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go
this view page code
@model albaraka.models.table_name @{ viewbag.title = "discussion_create"; layout = "~/views/shared/_layout.cshtml"; } <h2>discussion_create</h2> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>ab_discussion</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) @html.hiddenfor(model => model.discussion_id) <div class="form-group"> @html.labelfor(model => model.discussion_name, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.discussion_name, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.discussion_name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.discription, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textareafor(model => model.discription, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.discription, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="create" class="btn btn-default" /> </div> </div> </div> } <div> @html.actionlink("back list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
this model class
namespace project_name.models { using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.componentmodel.dataannotations.schema; public partial class table_name { [key] [databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.identity)] public int discussion_id { get; set; } public string discussion_name { get; set; } public string discription { get; set; } } }
this controller class
[httpget] public actionresult discussion_create() { return view(); } [httppost] [validateantiforgerytoken] public actionresult discussion_create(table_name discussion) { if (modelstate.isvalid) { db.table_name.add(discussion); db.savechanges(); return redirecttoaction("index"); } return view(discussion); }
i want insert record without user enter discussion id, that's why put in hidden field here, how can that?
there no need use hidden element in view, can remove it. should use try catch block in save changes can catch ef exceptions , act accordingly :
try{ db.table_name.add(discussion); db.savechanges(); } catch(exception ex){ //trace exception }
put breakpoint in debugger on exceptions if there , peek in db inserted. think code should work, don anticipate exceptions practice check them.
Comments
Post a Comment