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 8929923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:46:30+00:00 2026-06-15T08:46:30+00:00

When I create a new model and try to retrieve its ID , it

  • 0

When I create a new model and try to retrieve its ID, it always returns 0.

Why is that? I’m trying it after I add it to the corresponding set and context. As far as I know every time a new model is created, it recalls the DB and assigns a unique ID.

StoreGeneratedPattern value is set to Identify in the Model.edmx file.

  • 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-06-15T08:46:31+00:00Added an answer on June 15, 2026 at 8:46 am

    It will get number after context.SaveChanges(). Before it, there is no record in db for your new model object and it is not know id that will be generated on insert.

    Update:

    As example you have two tables: Books and Shelves. Any book can be placed in one of shelves and one shelve can contain many books. Typical one-to-many relationship.
    EF generates to you two classes in model:
    Class Book:

    public class Book
    {
        public int Id{get;set;}
        public string Title{get;set;}
        ....//some other properites
        public virtual Shelve Shelve{get;set;} //navigation property to Shelve where this book is placed
    }
    

    Class Shelve:

    public class Shelve
    {
        public int Id{get;set;}
        public string Name{get;set}
        ....//some other properties, as example address in library
        public virtual ICollection<Book> Books{get;set;}
    }
    

    If you want to create a book and place it in existing shelve you should:

    var book = new Book();
    //initialize properties of book
    var shelve = context.GetShelveById(shelveId); //get existing shelve from database by id
    shelve.Books.Add(book);
    context.SaveChanges();
    

    If you want to create new shelve and add to it new book then you can use something like next sample:

    var book = new Book();
    //initialize properties of book
    var shelve = new Shelve(); 
    //initialize properties of Shelve
    shelve.Books = new List<Book>();
    shelve.Books.Add(book);
    context.Add(shelve); //add shelve to context
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to test that a UserProfile model is created as a new User
I have a problem when i try to create new model field type -
I have the the following after create method in my record model that keeps
have some custom attr_accessor in my model. When I try to create a new
I want to create one new model class, which will be extended in all
I wanted to create a new property on a table in my model.. Basically
I have a model called Details, and two controller methods new and create. New
i am new in Cassandra db. need help to create data model. i have
I have this function in a Code Igniter model that creates a new video.
I create new blank solution. Add exist web site. Structure: Solution E:...\projectname Folders: bin

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.