Ryan
Ryan

Reputation: 5546

entity framework best practice

I have a class Car in my model. I want to save my Car objects into a database. I want to use entity framework.

Should I replace the initial Car class with the CarEF class generated by entity framework, and use that in my code? Or should I recreate my initial Car object from the one generated by EF like new Car{Year=myCarEfObject.Year}?

Upvotes: 0

Views: 920

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

EF as any other ORM framework is for API for persisting and retrieving objects from database. Your Car is a class which have to be saved to database and probably retrieved from database so you want to use EF to save directly the Car class. It means you have to define Car class as EF entity (in EF model). EF entity is class mapped to database => it can be retrieved and persisted. You can either use EF entities or POCO objects (EFv4).

Using different classes for working with object and for persisting object in most cases doesn't make sense. It is only additional layer of complexity.

Upvotes: 1

YeahStu
YeahStu

Reputation: 4052

I recommend replacing your initial car class with the CarEF class for most simple cases. There is more discussion about this topic in this answer.

Upvotes: 0

Related Questions