in this article, i will show you, crud operation code first approch in asp.net mvc entity framework
---model class---
public class Employee
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Salary { get; set; }
}
---download package---
Install-Package EntityFramework
---dbcontext class---
public class EmployeeDbContext:DbContext
{
//manual change connectionstring base class parameter
public EmployeeDbContext():base("DBCON"){}
public DbSet<Employee> employees { get; set; }
}
---web config---
<connectionStrings>
<add name="DBCON" connectionString="Data Source=.;Integrated Security=true;Initial Catalog=TESTDB" providerName="System.Data.SqlClient"/>
</connectionStrings>
---controller---
add mvc5 controller with view,using entity framework
---enable migration---
if any changes on model class than enable migration for update database, there is following code run on package manager console:
(i) Enable-Migrations
(ii) Add-Migration Init
(iii) Update-Database -Verbose
---model class---
public class Employee
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Salary { get; set; }
}
---download package---
Install-Package EntityFramework
---dbcontext class---
public class EmployeeDbContext:DbContext
{
//manual change connectionstring base class parameter
public EmployeeDbContext():base("DBCON"){}
public DbSet<Employee> employees { get; set; }
}
---web config---
<connectionStrings>
<add name="DBCON" connectionString="Data Source=.;Integrated Security=true;Initial Catalog=TESTDB" providerName="System.Data.SqlClient"/>
</connectionStrings>
---controller---
add mvc5 controller with view,using entity framework
---enable migration---
if any changes on model class than enable migration for update database, there is following code run on package manager console:
(i) Enable-Migrations
(ii) Add-Migration Init
(iii) Update-Database -Verbose
0 Comments
if you have any doubts , please let me know