Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It will get number after <code>context.SaveChanges()</code>. 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.</p> <p><strong>Update</strong>:</p> <p>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: <strong>Class Book</strong>:</p> <pre><code>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 } </code></pre> <p><strong>Class Shelve</strong>:</p> <pre><code>public class Shelve { public int Id{get;set;} public string Name{get;set} ....//some other properties, as example address in library public virtual ICollection&lt;Book&gt; Books{get;set;} } </code></pre> <p>If you want to create a book and place it in existing shelve you should:</p> <pre><code>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(); </code></pre> <p>If you want to create new shelve and add to it new book then you can use something like next sample:</p> <pre><code>var book = new Book(); //initialize properties of book var shelve = new Shelve(); //initialize properties of Shelve shelve.Books = new List&lt;Book&gt;(); shelve.Books.Add(book); context.Add(shelve); //add shelve to context context.SaveChanges(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload