开发者

entity framework best practice

开发者 https://www.devze.com 2023-02-23 09:31 出处:网络
I have a class Car in my model. I want to save my Car objects into a database. I want to use entity framework.

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 sh开发者_StackOverflowould I recreate my initial Car object from the one generated by EF like new Car{Year=myCarEfObject.Year}?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消