---
title: "Insert values to entity framework in asp.net mvc4"  
description: "Insert values to entity framework in asp.net mvc4"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1543/insert-values-to-entity-framework-in-asp-dot-net-mvc4  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# Insert values to entity framework in asp.net mvc4

In my [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) mvc4 sample i have table in the name of Sample with three [column as](https://www.mindstick.com/forum/159334/how-to-set-column-as-auto_increment-in-mysql) Name,Dept and Id.In this Id as [identity](https://www.mindstick.com/articles/13090/icon-the-identity-of-your-brand) and primary.I get a value for Name and Dept from user and [insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) that value to Sample table of [entity framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach).It [pass value](https://www.mindstick.com/forum/156804/how-to-pass-value-to-the-parameter-in-stored-procedure) '0' to Id.And i got an error as "Store [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update), insert, or delete statement affected an unexpected number of rows (0). [Entities](https://www.mindstick.com/blog/250/html-character-entities) may have been modified or [deleted](https://www.mindstick.com/forum/160487/how-to-remove-the-deleted-git-code-marker-in-scrollbar-in-visual-studio-2022) since entities were loaded. [Refresh](https://www.mindstick.com/forum/12865/how-to-refresh-dropdownlist-but-keep-old-selected-value-json) ObjectStateManager entries".

Please help me.

## Replies

### Reply by Anonymous User

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{    [Key]    [Required]    [Column(Order = 0)]    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] //auto increment id    public int Id { get; set; }    [Required]    public string Name { get; set; }    [Required]    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.


---

Original Source: https://www.mindstick.com/forum/1543/insert-values-to-entity-framework-in-asp-dot-net-mvc4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
