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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With