Defining the entities

Entities in EF Core are POCO classes, which means that you don't need to inherit from any base class to use them as your data model. Here is how the Product entity is defined:

public class Product
{
// Primary key
public int ProductId { get; set; }

// Value propertis
public User Owner { get; set; }
public string Title { get; set; }
public string Description { get; set; }

// Navigation properties - represents relationships
public Category Category { get; set; }
public City City { get; set; }
public IList<ProductMedia> Media { get; set; }
public DateTime PublishDate { get; set; }
}

I divided the class into three sections:

  • Primary key: Each entity must have a unique identity, called the Primary Key, which is composed from one or more values. By default, EF Core will set the value for the primary key (if it's a simple single value) when saving a new object.
  • Value properties: Properties that contain simple values, such as primitives and enums.
  • Navigation properties: Properties that make references to other entities or a collection of entities. These represent the relationships between the entities. 

 All the other entities we have in our GiveNTake app are defined in a similar way, and EF Core will map them to tables, columns, constraints, and other database objects, based on conventions and the explicit rules we can define. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.157.142