53

I keep hearing about the Entity Framework fluent-api but I am struggling to find a good reference on this. What is it?

We use the entity framework and the modeling tool provided. Is that all that is? Or is it something different?

Similarly, if it's not too broad a question, what is POCO? I know it stands for Plain Old CLR Objects, but what does that mean to me as somebody who uses EF already with the designer model tool? If that question is too vague then please ignore it. I'm just learning here and any information you are willing to provide is helpful.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
CatDadCode
  • 58,507
  • 61
  • 212
  • 318

5 Answers5

58

Entity Framework 4.1 introduces the code first approach of writing database models. This is also called POCO (Plain Old CLR Objects). The idea is that you can build your database from these classes, rather then building the database first and creating a model from that.

There are tons of good blog articles and MSDN documentation on this. A good place to start would be

http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx

http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx

Regards the fluent API, this is basically using the EF classes to build your database e.g.:

modelBuilder.Entity<Category>().HasKey(c => c.CategoryCode);

So you're manually stating that the Category table has a primary key named `CategoryCode'. You can also declare the PK like this:

public class Category
{
    [Key]    
    public int CategoryCode { get; set;}
}

The [Key] attribute comes from Data Annotations

Crwth
  • 2,038
  • 2
  • 15
  • 15
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • 2
    Jason, Am I missing something big if I decided to use Data Annotations instead of fluent api. I'm just starting with MVC and this topic is a bigger curve for me. – Crismogram Mar 18 '15 at 02:14
  • 5
    I don't believe you're missing out. But I would say that using the fluent API means you can put all the database setup in one method, rather than spreading that setup around in attributes that are associated with different classes. So it's a case of which way you prefer really. – Jason Evans Mar 18 '15 at 08:18
  • @JasonEvans good response, in the same way, I want to know what is the best tools to take all the controls of our database, fluent-api or data annotations? especially when we work on a big project with a lot of database structure changes? – Thamer Sep 29 '18 at 10:48
5

POCO stands for Plain Old CLR Object.

Article on Fluent API.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
1

You can also check the Code First Fluent API section on MSDN here http://msdn.microsoft.com/en-us/library/hh295844

0

Responding to your POCO question: in the application I'm currently working on I'm using POCO's to pass data to my Silverlight front end (EF just wasn't cutting it). Essentially, I use the entities that the EF modeler created, massage them into a serializable-friendly version, and then send them back and forth over the wire. POCO's are there to provide a layer of abstraction when needed. I think of it as an adaption of the DAO pattern to serialization, instead of using it for DB access like the DAO pattern normally does.

Eric Andres
  • 3,417
  • 2
  • 24
  • 40
0

See the paragraph on POCO classes in http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Essentially, in the context of EF, POCO classes are entity classes that do not inherit from the Entity Framework EntityObject class (which is what you get by default in Database First or Model First). As one of the other answers mentions, this makes it easier to serialize the objects, but also some development and automated testing methodologies prefer to work with objects that have no reference to the Entity Framework.

tdykstra
  • 5,880
  • 2
  • 23
  • 20