Work with EntityFrameworkCore and asp.net core
1. Select Required EntityFrameworkCORE Package
When you search on the EntityFrameworkCore in Nuget Package Manager you will get 100+ packages...
All the packages are derived from Microsoft.EntityFrameworkCore
2. You need to select Required package Then it will add the Dependencies
3. I am going to work with SqlServer and i am going to add Microsoft.EntityFrameworkCore.SqlServer
4. Now Create the DataBaseContext class and here i am going to create MyDataBaseContext class and extend DBContext
What you Need to Know - If Core performing Data access only using MODEL and model become entity class
6. Now i am going to perform data transaction and for that i need to create model
public class Student
{
[Key]
public Guid StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[Key] is for Define DataBase primary key column and [Key] attribute is in the System.ComponentModel.DataAnnotations NameSpace
7. Then you need to define this new model in your context class
8. Now your ready to perform transactions and those are same with .Net EntityFramework CRUD operations. Also you can use LINQ queries same as previous
using (var db = new MyDataBaseContext())
{
var students = db.Students
.Where(s => s.StudentId == Guid.NewGuid())
.OrderBy(s => s.FirstName)
.ToList();
}
using (var db = new MyDataBaseContext())
{
var student = new Student { StudentId=Guid.NewGuid(), FirstName="FirstName", LastName="LastName" };
db.Students.Add(student);
db.SaveChanges();
}
When you search on the EntityFrameworkCore in Nuget Package Manager you will get 100+ packages...
All the packages are derived from Microsoft.EntityFrameworkCore
2. You need to select Required package Then it will add the Dependencies
3. I am going to work with SqlServer and i am going to add Microsoft.EntityFrameworkCore.SqlServer
4. Now Create the DataBaseContext class and here i am going to create MyDataBaseContext class and extend DBContext
5. Put the DataBase Configuration you need to Override OnConfiguring Method
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=.;Initial Catalog=testdb;Integrated Security=True;");
}
Then you need to configure OptionBuilder method to say UseSqlServer and give the connection link to the DataBase
What you Need to Know - If Core performing Data access only using MODEL and model become entity class
6. Now i am going to perform data transaction and for that i need to create model
public class Student
{
[Key]
public Guid StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[Key] is for Define DataBase primary key column and [Key] attribute is in the System.ComponentModel.DataAnnotations NameSpace
7. Then you need to define this new model in your context class
8. Now your ready to perform transactions and those are same with .Net EntityFramework CRUD operations. Also you can use LINQ queries same as previous
using (var db = new MyDataBaseContext())
{
var students = db.Students
.Where(s => s.StudentId == Guid.NewGuid())
.OrderBy(s => s.FirstName)
.ToList();
}
using (var db = new MyDataBaseContext())
{
var student = new Student { StudentId=Guid.NewGuid(), FirstName="FirstName", LastName="LastName" };
db.Students.Add(student);
db.SaveChanges();
}
Comments
Post a Comment