Entity frame what is code first model first and database first

Posted by balajirnaukri-12656 under .NET Framework on 8/22/2013 | Points: 10 | Views : 3267 | Status : [Member] | Replies : 3
In entityframework what is Code First ,what is model First and What is database first? Which is the best to use.

Thanks and Regards
Balaji.R
ASP.NET Devloper
Solve-IT corp



Responses

Posted by: Bandi on: 8/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Code First:

In Code First approach, you avoid working with visual model designer (EDMX) completely. You write your POCO classes first and then create database from these POCO classes. Developers who follow the path of Domain-Driven Design (DDD) principles prefer to begin by coding their classes first and then generating the database required to persist their data.


Model First:

In Model First approach, you create Entities, relationships, and inheritance hierarchies directly on the design surface of EDMX. So in Model First approach, when you add ADO.NET Entity Data Model, you should select ‘Empty Model’ instead of ‘Generate from database’.


Database First:

In Database First approach, firstly database is created from back-end and then we generate EDMX file from existed database by using ADO .NET Entity Data Model....

Refer the following link for detailed explanation
http://www.entityframeworktutorial.net/code-first-with-entity-framework.aspx

Most of the time we have used code first model for projects...

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 8/22/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Code First:
* Code First is especially popular in the early stages of a brand new project (the so-called “green field” scenario)
* Ideally you don’t have to be concerned with existing data and schemas; you are working with test data and are free to create and destroy databases at will.
* You don’t create or update database schemas in the early phase of the project. You let the Entity Framework create (and recreate) the database for you.
* You just work with the small, simple entity classes that you wrote yourself, changing them as you go, unencumbered by external tools, XML files, or databases. It’s fast, it’s clean, and it’s transparent.
* Entity class simplicity is another advantage of Code First. In Code First you always write the class yourself. Your class has only the code that you decide is necessary and appropriate. You write the class members in the style you prefer and in the order that makes sense to you.

DataBase First:
* Database First is popular in so-called “brown field” scenarios with developers who build applications that access existing production databases holding large numbers of tables and columns.
* You will let EF create entities for you and after modification of mapping you will generate POCO entities.
* Database First shines when managing a large, complex database schema with numerous unconventional mapping requirements … but you’re a prisoner of the EDMX and the EDM Designer. Code First shines when you’re making lots of small changes iteratively to a model that aligns well with your database.

Model First:
* In the Model First approach, the database model is created first using the ORM designer in Visual Studio.
* Once the model consisting of entities and relationships has been designed, the physical database will be generated from the model.
* It gives you the flexibility to delay the creation of the database. You can design the structure independently, and see different possibilities of improving it. Later you can generate the physical structure. Good for new systems or new capabilities in existing systems.

Mark as answer if satisfied.........

Thank You,
Kavya Shree M.


Kavya Shree Mandapalli

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 8/22/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Database First:
If you already have a database, the Entity Framework can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns. The information about your database structure (store schema), your data model (conceptual model), and the mapping between them is stored in XML in an .edmx file. Visual Studio provides the Entity Framework designer, which is a graphical designer that you can use to display and edit the .edmx file. The sections Getting Started With the Entity Framework and Continuing With the Entity Framework in the Web Forms tutorial series use Database First development.

Model First:
If you don't yet have a database, you can begin by creating a model using the Entity Framework designer in Visual Studio. When the model is finished, the designer can generate DDL (data definition language) statements to create the database. This approach also uses an .edmx file to store model and mapping information. The What's New in the Entity Framework 4 tutorial includes a brief example of Model First development.

Code First:
Whether you have an existing database or not, you can code your own classes and properties that correspond to tables and columns and use them with the Entity Framework without an .edmx file. That's why you sometimes see this approach called code only, although the official name is Code First. The mapping between the store schema and the conceptual model represented by your code is handled by convention and by a special mapping API. If you don't yet have a database, the Entity Framework can automatically create the database for you, or drop and re-create it if the model changes. This tutorial series uses Code First development.

The data access API that was developed for Code First is based on the
DbContext

POCO (Plain Old CLR Objects)

By default, when you use the Database First or Model First development approaches, the entity classes in your data model inherit from the EntityObject class, which provides them with Entity Framework functionality. This means that these classes technically aren't persistence ignorant and so don't conform fully to one of the requirements of domain-driven design. All development approaches of the Entity Framework can also work with POCO (plain old CLR objects) classes, which essentially means that they are persistence-ignorant because they don't inherit from the
EntityObject class.

Mark as answer if satisfied.........

Thank You,
Kavya Shree M.




Kavya Shree Mandapalli

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response