DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

Types of Entities in Entity Framework

In Entity Framework (EF), there are mainly three types of entities that are commonly used:

  1. Persistent Entities: These entities represent the data model and correspond to tables in the database. They are mapped to database tables and have a one-to-one mapping with the underlying database schema. Persistent entities are typically annotated with attributes or configured using fluent API to define their mappings to database tables.

  2. Complex Types: Complex types are classes that are used as properties within persistent entities. They are not mapped to their own tables in the database but rather stored as part of the parent entity's table. Complex types are used to encapsulate a group of related properties that logically belong together.

  3. Enumerations (Enums): Enums are a special type of entity in EF that represent a fixed set of named values. They can be used to define properties on persistent entities that have a limited set of possible values. Enums are often used to provide more meaningful and type-safe representations for certain properties.

These three types of entities are the basic building blocks used in Entity Framework to model and interact with data in a database.

Certainly! Here are the types of entities in Entity Framework with examples:

  1. Persistent Entities:
    • Person: Represents a person with properties like Id, Name, Age, etc.
   public class Person
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public int Age { get; set; }
   }
Enter fullscreen mode Exit fullscreen mode
  • Product: Represents a product with properties like Id, Name, Price, etc.
   public class Product
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public decimal Price { get; set; }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Complex Types:
    • Address: Represents an address with properties like Street, City, State, etc. Used as a complex type within another entity.
   public class Address
   {
       public string Street { get; set; }
       public string City { get; set; }
       public string State { get; set; }
   }
Enter fullscreen mode Exit fullscreen mode
  • OrderItem: Represents an item in an order with properties like ProductId, Quantity, Price, etc. Used as a complex type within the Order entity.
   public class OrderItem
   {
       public int ProductId { get; set; }
       public int Quantity { get; set; }
       public decimal Price { get; set; }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Enumerations (Enums):
    • Gender: Represents the gender of a person with values Male, Female, and Other.
   public enum Gender
   {
       Male,
       Female,
       Other
   }
Enter fullscreen mode Exit fullscreen mode
  • OrderStatus: Represents the status of an order with values Pending, Shipped, Delivered, etc.
   public enum OrderStatus
   {
       Pending,
       Shipped,
       Delivered
   }
Enter fullscreen mode Exit fullscreen mode

These are just examples to illustrate the different types of entities in Entity Framework. In practice, you would define your own entities based on your application's requirements and domain model.

Top comments (0)