Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Entity Framework fluent api?

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.

like image 998
Chev Avatar asked Jun 13 '11 15:06

Chev


People also ask

Which class is used as fluent API in EF core?

In Entity Framework 6, the DbModelBuilder class acts as a Fluent API using which we can configure many different things. It provides more options of configurations than Data Annotation attributes.

Is fluent API better than data annotation attributes?

The fluent API is considered a more advanced feature and we would recommend using Data Annotations unless your requirements require you to use the fluent API. But in my opinion you reach the limitations of DataAnnotations very quickly (except perhaps for extremely simple object models).

What is Entity Framework used for?

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.

What is the use of OnModelCreating?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


1 Answers

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

like image 113
Jason Evans Avatar answered Sep 18 '22 00:09

Jason Evans