- Code -First Approach
- Model -First Approach
- Database First Approach
We use Code First approach in our project and would never think about data first unless we have to integrate with an existing database.
- Instead of starting by figuring out what database looks like, we start by figuring out what our application does.
- We don't have to reconcile up-front database design with the functionality, API's etc. Instead, we do all of the design activities together.
- The cost of "pre-design" can be high especially if something has changed in the overall goals of the application that causes the database model to change significantly.
In this post I'm going to discuss about Code- First Approach.
Code First Approach
Entity Framework introduced Code-First approach from Entity Framework 4.1. With the Code-First approach, we can focus on the domain design and start creating classes as per our domain requirement rather than design our database first and then create the classes which match our database design. Code-First APIs will create the database on the fly based on your entity classes and configuration.
How to create a db in Code-First Approach
Let's assume that we
want to create a simple application ABC Store. Users of this application should
be able to add or Update Item and Category.
Instead of designing
database tables first, let's start creating classes for our shop domain, as and
when needed. First, we will create two simple Item and Standard classes where
every Item is associated with one price as shown below.
public class Item
{
public Item()
{
}
public int ItemID { get; set; }
public string
ItemName { get; set; }
public Category
Category { get; set; }
}
The Category class
should be able to accommodate multiple Item as shown below.
public class Category
{
public Category()
{
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public ICollection<Item> Items { get; set; }
}
Now, we are done with
the initial domain classes for our sales application.
Code-First approach also requires context class which should be derived from “DbContext”.
Code-First approach also requires context class which should be derived from “DbContext”.
Create a context class
as shown below, which derives from DBContext class and exposes DbSet properties
for the types that you want to be part of the model
DbSet is a collection of
entity classes (aka entity set), so we have given property name as plural of
entity name
namespace ABC.RPoS.Sample
{
public class SalesContext: DbContext
{
public SalesContext(): base()
{
}
public DbSet<Item> Items { get; set; }
public DbSet<Category> Categories{ get; set; }
}
}
Now, we are done with
the required classes for the code-first approach. We will now add Item using
context class as shown below.
references :
class Program
{
static void Main(string[] args)
{
using (var ctx = new SalesContext())
{
Item item = new Item() { ItemName = "New Item" };
ctx.Items.Add(item);
ctx.SaveChanges();
}
}
}
https://www.youtube.com/watch?v=kbH-rqMl8cE
ReplyDeleteYou tube video for more assistance