Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an Entity? Why is it called Entity?

What is the deal with Entities (when talking about the Entity Framework)? From what I understand, it is pretty much an in memory representation of a data store like sql tables. Entities are smart enough to track changes and apply those changes to the data store. Is there anything more to it?

Thanks in advance.

like image 370
rkrauter Avatar asked May 07 '10 01:05

rkrauter


1 Answers

It comes from the field of systems engineering where they use the Entity Relationship Diagram tool to design systems.

What they do is start by laying out the entities (things like customers, purchase orders, purchase order line items, etc.). Each entity is a conceptual thing. Then you generally create an entity class for each entity, and a database table as your backing store. What the entity framework allows you to do is take a database schema, assuming it's already a good representation of your entity diagram, and automatically generate entity classes to encapsulate those.

Since an entity is an abstraction of the real things in your system, by creating a class for each entity, it's a nice way to architect your system to put the relevant code for each entity in the right place. The way the framework works is that it creates two files: an auto generated file that maps to the fields of the database, and a "custom" file where you can put custom logic. These are defined as partial class files, so they're joined together at compile time to form one class, but it keeps your data access and business logic nice and separate. You can then re-generate the auto-generated partial side of the class any time your database schema (entity relationship diagram) changes.

Other nifty features:

  • The framework understands the relationships between entities, so once you have a "purchase order" entity, you can automatically grab all the purchase order line items (it's just a property of the entity that returns a collection)
  • You can implement entity inheritance (for instance, an employee could inherit from contact)
  • Linq to Entities!
like image 113
Scott Whitlock Avatar answered Sep 28 '22 08:09

Scott Whitlock