Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:20:30+00:00 2026-05-16T16:20:30+00:00

I follow by entity framework example : http://msdn.microsoft.com/en-us/library/bb399182.aspx and I have problem with Identity

  • 0

I follow by entity framework example :

http://msdn.microsoft.com/en-us/library/bb399182.aspx

and I have problem with Identity Columns.

Here is part of code of creating database:

CREATE TABLE [dbo].[Person](
    [PersonID] [int] IDENTITY(1,1) NOT NULL,
    [LastName] [nvarchar](50) NOT NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [HireDate] [datetime] NULL,
    [EnrollmentDate] [datetime] NULL,
 CONSTRAINT [PK_School.Student] PRIMARY KEY CLUSTERED 
(
    [PersonID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

In VS 2010 I build .edmx and at model I see that Person StoreGeneratedPattern is set to Identity.

But when I want to create Person, by :
alt text

Why I must put id, if this column is autoincrement?

EDITŁ

I thought that I found the way to resolve my problem by:

var schoolContext = new SchoolEntities();

            schoolContext.AddToPeople(new Person() { LastName = "Gates", FirstName = "Bil" });

            schoolContext.SaveChanges();

because it added Bill to persons, but…because PersonID is not nullable, and it inserted him with id 0. When I tried add another person the same way of course I get error about primary key 🙂

So still with nothing…

Any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:20:30+00:00Added an answer on May 16, 2026 at 4:20 pm

    I cannot tell you why the EF team chose to do it this way – my only guess would be that the code generation that create the CreatePerson method doesn’t check to see whether or not the ID is an autoincrement and just creates a method that will work in any circumstance – whether the ID is autoincrement or not.

    If this really bothers you, you can also benefit from the fact that the generated entity class Person is defined as a partial class, so you can extend it easily. Create a new class file called e.g. PersonEx.cs and extend that partial class:

    public partial class Person 
    {
        public static Person CreatePerson(string lastName, string firstName)
        {
            return CreatePerson(-1, lastName, firstName);
        }
    }
    

    Now, you can easily create your Person entities without specifying an ID, and add them to your data:

     using(SchoolEntities context = new SchoolEntities())
     {
         Person newPerson = Person.CreatePerson("Gates", "Bill");
         context.AddToPeople(newPerson);
    
         context.SaveChanges();
    
         int newID = newPerson.PersonID;
     }
    

    It’s not a perfect solution – but it should work just fine (at least it does for me).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to follow this example: Creating Model Classes with the Entity Framework
I'm using Entity Framework Code First. Usually I have no problem, but for a
I have a simple LINQ query on ADO.NET Entity Framework as follows var result
Basically each entity would have a collection of feeding behaviors that it would follow.
As a follow up to this question , I have a parent entity that
I am new to play framework. tried to follow the tutorial http://www.playframework.org/documentation/1.2.4/guide2 when i
I'm pretty new to MVC 2 using the Entity Framework. I have two tables
I'm using the Microsoft.Data.Entity.CTP (in the Entity Framework CTP) under the .NET 4 framework
Now that I have built a database. Visual Studio 2008 SP1’s ADO.NET Entity Framework
I try to create database from entity framework code first follow with this tutorial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.