Creating Database By in ASP.NET MVC by using Entity Framework Code First Approach
Take one webapplication
Now change the authentication
click on 'Change Authentication' button
Click OK
Add this two namespaces
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Products")]
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public DateTime ManufactureDate { get; set; }
public Int64 Cost { get; set; }
}
[Table("Products")] :- Products is a table name.
[Key] :- Defines it is a Primary Key.
Now Right click on model add another class and give a name it Company.
Add that two name spaces what we add on product class
and copy this code
[Table("Company")]
public class Company
{
[Key]
public int CompanyID { get; set; }
public string Name { get; set; }
public Product Product { get; set; }
}
public Product Product { get; set; } => This is a foreign key relation it makes what are the primary keys in product class are become a foreign keys in Company class
Also add same relation in product class
Now goto Model add another class name it ShoppingDbcontext.
"The class that derives DbContext is called context class in entity framework. DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database.DbContext is the primary class that is responsible for interacting with the database."
using System.Data.Entity; add this name space.
and inherit the shoppingcontext to DbContext
eg:-
public class ShoppinDbcontext : DbContext
Copy and paste below code:
public class ShoppinDbcontext : DbContext
{
public ShoppinDbcontext()
:base("conStr")
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Company> Company { get; set; }
}
conStr :- is a ConnectionString name what we give in Web.config.
Now Right click on Controller add an empty MVC Controller.
using DemoMvc.Models; add this namespace
DemoMvc:- ProjectName
Now Create an object for Dbcontext and add all model classes to this controller.
ShoppinDbcontext db = new ShoppinDbcontext();
With this object we can add model classes to the controller.
copy below code
public class DefaultController : Controller
{
ShoppinDbcontext db = new ShoppinDbcontext();
// GET: Default
public ActionResult Index()
{
db.Products.ToList();
db.Company.ToList();
return View();
}
}
Now Right click on Index() Controller and select 'AddView' Option.
Now change the authentication
click on 'Change Authentication' button
Click OK
Now Project is Created
Now Open Web.Config
Following Connectionstring is created Automatically
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-DemoMvc-20180325102616.mdf;Initial Catalog=aspnet-DemoMvc-20180325102616;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Just Delete it and paste the below Connection String
<connectionStrings>
<add name="conStr" providerName="System.Data.SqlClient" connectionString="Data Source=My-PC; Initial Catalog=OnlineShopping; Integrated Security=True; MultipleActiveResultSets=True;"></add>
</connectionStrings>
Up to know we don't create Database in sql-server.
Here Initial Catalog=OnlineShopping; => is a database name know we creating.
Remove that Name and give what you want (or) Keep that
Next
My-PC; => it is my local sql-server name.
Web.config is completed.
Now Right click on "Model" add a Class and give a name it 'Product'.
Add this two namespaces
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Products")]
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public DateTime ManufactureDate { get; set; }
public Int64 Cost { get; set; }
}
[Table("Products")] :- Products is a table name.
[Key] :- Defines it is a Primary Key.
Now Right click on model add another class and give a name it Company.
Add that two name spaces what we add on product class
and copy this code
[Table("Company")]
public class Company
{
[Key]
public int CompanyID { get; set; }
public string Name { get; set; }
public Product Product { get; set; }
}
public Product Product { get; set; } => This is a foreign key relation it makes what are the primary keys in product class are become a foreign keys in Company class
Also add same relation in product class
Now goto Model add another class name it ShoppingDbcontext.
"The class that derives DbContext is called context class in entity framework. DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database.DbContext is the primary class that is responsible for interacting with the database."
using System.Data.Entity; add this name space.
and inherit the shoppingcontext to DbContext
eg:-
public class ShoppinDbcontext : DbContext
Copy and paste below code:
public class ShoppinDbcontext : DbContext
{
public ShoppinDbcontext()
:base("conStr")
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Company> Company { get; set; }
}
conStr :- is a ConnectionString name what we give in Web.config.
Now Right click on Controller add an empty MVC Controller.
using DemoMvc.Models; add this namespace
DemoMvc:- ProjectName
Now Create an object for Dbcontext and add all model classes to this controller.
ShoppinDbcontext db = new ShoppinDbcontext();
With this object we can add model classes to the controller.
copy below code
public class DefaultController : Controller
{
ShoppinDbcontext db = new ShoppinDbcontext();
// GET: Default
public ActionResult Index()
{
db.Products.ToList();
db.Company.ToList();
return View();
}
}
Now Right click on Index() Controller and select 'AddView' Option.
Just Click on Add Button.
Now Index.cshtml is created
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
Just Reneame <h2>Index</h2>
Like <h2>DataBase Created</h2>
Now Just Hit F5
Database is Created
go to Sql-Server and check the database name.
Comments
Post a Comment