0

I have table:

CREATE TABLE [dbo].[Items] 
(
     [Id] INT IDENTITY (1, 1) NOT NULL,
     PRIMARY KEY CLUSTERED ([Id] ASC)
);

Id is auto increment and I am creating new record like this:

var Context = new Entities();
var Item = new Items();
Context.SaveChanges();

But in the table there is no new record. What am I doing wrong?

1 Answer 1

3

You have to add the item into the entity set.

using (var dbCtx = new Entities())
{
   var myItem = new Items();
   dbCtx.Items.Add(myItem);
   dbCtx.SaveChanges();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I get this error: "An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code Additional information: An error occurred while updating the entries. See the inner exception for details."
You are most likely trying to insert a duplicate. Look at the the following SO answer: stackoverflow.com/questions/20762923/…
I updated my answer. You need to declare the item correctly. My original response was specifically related to the fact that you weren't adding the item to the entity set.
Yeah, you are right. Sorry, I know, where is the issue. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.