2

What I think I understand (Please correct me if I am wrong)

  • POCO - Plain Old C# Objects. They are simple classes that dont inherit from anything?
  • STE - Self-Tracking Entities. they track changes on the individual entity objects so that when we do context.SaveChanges(); the changes on the entity objects are registered. This does not work on collections?

What I want to know?

  • If my context inherits from ObjectContext, does that mean I have POCO or STE? Similarly, is DbContext POCO or STE?
  • What does EF5 generate by default? POCO entities or STE? (I am using Model First)

Also can somebody give me some code examples representing these two types of entities?

Joe Slater
  • 2,483
  • 6
  • 32
  • 49

2 Answers2

2

If my context inherits from ObjectContext, does that mean I have POCO or STE? Similarly, is DbContext POCO or STE?

If your context inherits from the ObjectContext class so you're probably have STE and DbContext working against POCOs. look here.

What does EF5 generate by default (Model first)? POCO entities or STE?

Model First, POCO and STE are 3 different things. In Entity Framework you are free to choose the best approach that compatible with your needs.

It could be:

  • Code-First
  • Model-First
  • Database-First

POCOs - Plain Old CLR Objects are "clean" classes that doesn't interspersed with database access functionality etc. and are considered as persistence-ignorance entities.

STE - Self Tracking Entities that are well aware to their persistence mechanism and considered as aware-persistnace.

Update: If you're working in the Model-First approach and would like to convert your entities (STE) to POCOs, you can use the EF 5.x DbContext Fluent Generator for C#

Community
  • 1
  • 1
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
2

The IDE data designer, by default, creates an .edmx file which in earlier versions of Visual Studio (2008 and 2010) by default uses t4 templates that generate STE's by default and a context that derives from Object Context. In VS 2012, this was changed to generate POCO's by default with a context that derives from DbContext.

You can change this behavior by downloading a new code generator using NuGet.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • thx. exactly what I wanted to know. Can you confirm another thing for me? If I have STE, does that mean the entity classes must inherit atleast from either ObjectEntity or DbEntity? Also, if I have STE, does that simply mean that I do not have to call context.SaveChanges(); because the changes are being tracked automatically? Thx – Joe Slater Jul 05 '13 at 08:38
  • @AnkurSharma - You always have to call SaveChanges, because the framework doesn't know when you want to apply changes. You may, for instance, not want to save changes because some event occurred. STE's are deprecated, and Microsoft does not suggest using them anymore. – Erik Funkenbusch Jul 05 '13 at 19:06