Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the purpose of OnModelCreating in EF4 Code-First?

I was curious about what the purpose of OnModelCreating in EF4 Code-First context class? How does it work?

like image 574
Rushino Avatar asked Jan 21 '11 12:01

Rushino


2 Answers

Here is a nice article in ADO.NET Team Blog.
In short, this event is mainly for Fluent mapping.

like image 155
Devart Avatar answered Nov 09 '22 14:11

Devart


OnModelCreating gives you access to a ModelBuilder instance that you can use to configure/customize the model.

As the previous answer mentions, you will typically use the code-first fluent API within it. Gil Fink has posted what IMHO is a clearer explanation than the article cited in the previous answer.

For background info, Guthrie has a nice intro article on EF Code-First, and if you are wondering why its referred to as "fluent API", look here.

like image 31
BitMask777 Avatar answered Nov 09 '22 14:11

BitMask777