Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't form some simple POCO's to use with "Code First" Entity Framework, please check for mistake
    primarykey
    data
    text
    <p>So I decided to go with the code first/DbContext approach, but already have an existing database file. Nothing complex, so I am thinking I can just create the DbContext derived container class with DbSets for the respective POCO's, create the connection string to my database and I should be set. However I believe I am having difficulties properly declaring the properties in my entity classes since I am getting errors when trying access an object through the navigational properties. Usually telling me <code>Object reference not set to an instance of an object</code> when I try <code>context.Products.Find(1).Category.CATNAME;</code> etc. Also tried declaring the collection properties with virtual keyword to no avail.</p> <p><strong>Some specifics of the database schema are:</strong></p> <blockquote> <p>In Categories table the PCATID is a foreign key to the CategoryID in the same Categories table and can be null.</p> <p>Both CategoryID and RootCategoryID in Products table can be null and are both foreign keys to CategoryID in the Categories table.</p> </blockquote> <p>I am testing things at the moment but will be setting a lot of the fields to non null types eventually.</p> <p><img src="https://i.stack.imgur.com/PW7KM.jpg" alt="enter image description here"></p> <p>Here are my entity POCO's and the entity Dbset container class:</p> <pre><code> public class Category { [Key] public int CategoryID { get; set; } public string CATNAME { get; set; } public int PCATID { get; set; } public ICollection&lt;Category&gt; Categories { get; set; } public ICollection&lt;Product&gt; Products { get; set; } } public class Product { [Key] public int ProductID { get; set; } public int CategoryID { get; set; } public int RootCategoryID { get; set; } public string Name { get; set; } public string ShortDescription { get; set; } public string LongDescription { get; set; } public string Keywords { get; set; } public decimal ListPrice { get; set; } public Category Category { get; set; } } public class EFDbContext: DbContext { public DbSet&lt;Product&gt; Products { get; set; } public DbSet&lt;Category&gt; Categories { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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