What is Entity Framework fluent api?

前端 未结 5 1237
梦毁少年i
梦毁少年i 2021-01-30 00:45

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

5条回答
  •  后悔当初
    2021-01-30 01:38

    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().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

提交回复
热议问题