Introduction
this is my fist article but after reading this article you are getting what is entity framework 4.1 and why say it's to "code fist" and how it is make easy to do database work in your any .net application here i give you only one asp.net mvc 3 example but you can use that in same in you any .net application (like silverlight, wpf ,asp.net 4 etc)benefits of mvc 3(here we are see)
1.no need to create database here database are automatable garneted by .net
2.no need to create view(page) for insert update and delete in application
3.mvc 3 is full support for HTML5 CSS3 and JQuery
and many more
Background
fist of all before reading this article you have to some basic knowledge of .net architectural, oops and asp.net mvc 3basic software tool to run this simple application in you pc
- visual studio 2010
- Asp.net mvc 3 tools
- entity framework 4.1
Using the code
here we create one asp.net mvc 3 empty project
1. fist start visual studio then select new project and select asp.net mvc 3 application give MyFistEF name to application
press ok that ask you which type of project you want to select empty project and view engine is Razor than press ok button
then you need to add some class in to your model in you solution explorer right click to "Model" folder and add new class
here we create simple example for shop and book one shop have many book create one class "book"
using System;
using System.Linq;
using System.Text;
namespace MyFistEF.Models
{
public class Book
{
public int BookID { get; set; }
public string BookName { get; set; }
public string AuthorNAme { get; set; }
}
}
same as book class create bookshop classusing System.Linq;
using System.Text;
namespace MyFistEF.Models
{
public class Book
{
public int BookID { get; set; }
public string BookName { get; set; }
public string AuthorNAme { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyFistEF.Models
{
public class BookShop
{
public int BookShopId { get; set; }
public string ShopName { get; set; }
public IList<Book> BookList { get; set; }
}
}
in shop class we take one IList for book that is for one shop have many book when in shop class we take one IList for book that is for one shop have many book
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MyFistEF.Models
{
public class DataContext :DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<BookShop> Shops { get; set; }
}
}
now add new Controllers right click to "Controllers" and select add => Controller
giver name to this controller to "homeController" then template option select "controller with read/write actions and views, using Entity Framework" that is create view for insert update and list view for selected model class here we select "Book" model class then last select Date context class then press add
you application is ready to run when application is run the entity framework is create data base and it's table automatically it's also give Pk and Fk in to table and give relationship also
No comments:
Post a Comment