Insert values to entity framework in mvc4

Total Post:135


 3546  View(s)
Rate this:

In my mvc4 sample i have table in the name of Sample with three column as Name,Dept and Id.In this Id as identity and primary.I get a value for Name and Dept from user and insert that value to Sample table of entity framework.It pass value '0' to Id.And i got an error as "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries".

Please help me.

  1. Re: Insert values to entity framework in mvc4

    Hey Chintoo!

    In EF code-first this is done by data annotations. Make sure you recreate your table (or database) after changing the scheme.

    public class Sample
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] //auto increment id
        public int Id { get; set; }
        public string Name { get; set; }
        public int Dept { get; set; }

    You don't need to pass an integer for Id. EF will resolve this automatically. Just create the object by filling in the name and the dept. By saving the changes to the database, EF will automatically fill in the auto-generated Id.

      Modified On Mar-31-2018 02:10:03 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.