Any Fluent API tutorials that use EF Database-First approach to explain the subject?
Asked Answered
C

3

9

There are many tutorials on Fluent API, but they all explain it using Entity Framework Code-First code examples. Since I don't know Code-First, do you know of any Fluent API tutorials that would explain the subject using EF Database-First approach?

Thank you

Cyzicus answered 19/4, 2012 at 18:5 Comment(0)
E
10

There are no tutorials which would explain the Fluent API together with Database-First approach because Fluent API is made only for Code-First approach. You don't need the Fluent API if you want to create your model via Database-First.

Fluent API (together with Code-First data annotations and conventions) is a tool to define model details in code, such as string length, if a property is required or the type of relationship - many-to-many, one-to-many, etc. When using Database-First or Model-First the EDMX file has the same purpose - it contains all the details and mapping definitions of your model. Fluent API (+ data annotations and conventions) replaces the EDMX file only when using Code-First.

If you create the model via Database-First or Model-First, you will have an EDMX file representing your model. You can apply the T4 DbContext Generator to this EDMX file. The generated files have two characteristics being different from Code-First:

  • The generated connection string contains a section refering to the EDMX metadata which will be embedded into your assembly:

    connectionString="metadata=res://*/Model.csdl
                              |res://*/Model.ssdl
                              |res://*/Model.msl;
                      ..."
    
  • The generated context DbContext will have an overridden OnModelCreating method which just throws an exception:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    

As long as you leave the metadata section in the connection string, EF won't even call OnModelCreating or any code in Fluent API in this method. The metadata section tells EF that your model is DB- or Model-First and that the metadata are defined in the embedded EDMX and not in Fluent API.

However, you can remove the metadata section from the connection string, remove the UnintentionalCodeFirstException and write code with Fluent API in OnModelCreating. You can follow this procedure to create an initial model via Database-First and then build on this initial model for further development with Code-First.

At this point you are not working anymore with Database-First, but Code-First instead and everything you read about Fluent API is valid for you.

Esemplastic answered 19/4, 2012 at 21:39 Comment(3)
I will mark your post as answered in a day or two since you've already answered my original question, but if you find the time - I'm currently learning about DBContext and its Validation API. Anyways, I assume Validation API can also be used with Database-First, even though Database-First doesn't use Fluent-API?Cyzicus
@user702769: Hm, good question... I really don't know. I've never used the validation feature with database-first. Maybe ask a new question about this.Esemplastic
will do that. Thank you very much for your helpCyzicus
C
3

There is an interesting post about accomplishing some of the “Database-First” objectives without employing the actual “Database-First” methodology per se.

http://agilenet.wordpress.com/2011/04/11/entity-framework-4-1-rc-with-an-existing-database/

The author uses “Code-First” and “Fluent API”, but disables the auto-generation and seeding of databases and tables.

He shares a sample that shows “how to create an entity model, then manually create your database and then map those entities to your database. Finally it shows using the DatabaseContext to save and retrieve entities”.

The part where he creates a “configuration class for each entity which maps between the entity and the database” is pretty cool. That step replaces the “edmx” files that would be generated when employing a formal “Database-First” approach.

Corenda answered 20/2, 2013 at 17:31 Comment(0)
Z
0

This tutorial in a 6-part Database First tutorial series includes some fluent API examples:

http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control-part-3-sorting-and-filtering

Zhao answered 19/4, 2012 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.