The project code refer office document, you can see : https://learn.microsoft.com/zh-cn/ef/【项目可以参考官方文档,参考地址】
namespace EFCoreConsoleApp.Models
{
public class Product
{
public int Id { set; get; }
public string Name { set; get; } = null!;
[Column(TypeName="decimal(6,2)")]
public decimal Price { get; set; }
}
}
namespace EFCoreConsoleApp.Models
{
public class OrderDetail
{
public int Id { get; set; }
public int Quantity { get; set; }
public int ProductId { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; } = null!;
public Product Product { get; set; } = null!;
}
}
namespace EFCoreConsoleApp.Models
{
public class Order
{
public int Id { get; set; }
public DateTime OrderPlaced { get; set; }
public DateTime? OrderFulfilled { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; } = null!;
public ICollection OrderDetails { get; set; } = null!;
}
}
namespace EFCoreConsoleApp.Models
{
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string? Address { get; set; }
public string? Phone { get; set; }
public string? Email { get; set; }
public int? sex { get; set; }
public ICollection Orders { get; set; } = null!;
}
}
namespace EFCoreConsoleApp.Data
{
public class MyLocalContext : DbContext
{
public DbSet Customers { get; set; } = null!;
public DbSet Orders { get; set; } = null!;
public DbSet Products { get; set; } = null!;
public DbSet OrderDetails { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Get the connection string pattern from : https://learn.microsoft.com/en-us/ef/core/
optionsBuilder.UseSqlServer("Server=localhost;Database=EFCore;User Id=sa;Password=xxxx123");
}
}
}
The EF Core tools help with design-time development tasks. They are primarily used to manage Migrations and to scaffold a DbContext and entity types by reverse enginerring the schema of a database.
Install the Package manager Console tools by runing the following command in Package Manager Console.【执行安装命令】
Install-Package Microsoft.EntityFrameworkCore.Tools
Update the tools by running the following command in Package Manager Console.
Update-Package Microsoft.EntityFrameworkCore.Tools
Verify taht the tools are installed by running the following command:
Get-help about_EntityFrameworkCore
OR
dotnet ef
can be installed as either a global ot local tools. I recommand you to install it as global tools, because most developers do like that.【Mac 命令行执行方式】
Using the following command to install the tools:
dotnet tool install --global dotnet-ef
Using the following command to update the tools:
dotnet tool update --global dotnet-ef
Before you can use the tools on a special project, you will need to add the Microsoft.EntityFrameCore.Design package to it:
dotnet add package Microsoft.EntityFrameworkCore.Design
Verify installation:【验证安装效果】
After finished the prepartion, you can generate table by running the following command in order:
dotnet ef migrations add InitialCreate
dotnet ef database update
You can also refer Migration Commands to implement the same result by running command in your VS IDE.
Add-Migration InitialCreate
Update-Database
Verify the local database tables:【得到创建的表结果】
Here are some demo code to insert or update some data to your database, you can try that and help you release how to do CRUD with EFCore:
using MyLocalContext context = new MyLocalContext();
# region Add Products
// Create a new product
Product veggiSpecial = new Product()
{
Name = "Veggie Special Pizza",
Price = 9.9M
};
// Save product
context.Products.Add(veggiSpecial);
Product deluxeMeat = new Product()
{
Name = "Deluxe Meat Pizza",
Price = 12.99M
};
context.Products.Add(deluxeMeat);
context.SaveChanges();
#endregion
#region display products
var products = context.Products
.Where(p => p.Price > 10.0M)
.OrderBy(p => p.Price);
Console.WriteLine("Use function method way:");
foreach (Product p in products)
{
Console.WriteLine($"Id: {p.Id}");
Console.WriteLine($"Name: {p.Name}");
Console.WriteLine($"Price: {p.Price}");
Console.WriteLine(new string('-', 20));
}
#endregion
#region linqProduct
var productsLinq = from product in context.Products
where product.Price >= 10.0M
orderby product.Price descending
select product;
Console.WriteLine("Use Linq way:");
foreach (Product p in productsLinq)
{
Console.WriteLine($"Id: {p.Id}");
Console.WriteLine($"Name: {p.Name}");
Console.WriteLine($"Price: {p.Price}");
Console.WriteLine(new string('-', 20));
}
#endregion
#region update product
var veggiSpecialUpdate = context.Products
.Where(p => p.Name == "Veggie Special Pizza")
.FirstOrDefault();
if (veggiSpecialUpdate is Product)
{
veggiSpecialUpdate.Price = 11.11M;
}
context.SaveChanges();
#endregion
Result as sinpshot:【展示效果】
You can get the first EFCore project by follow the blob step by step. We can have a discussion in private If you meet any problems when create your demo code.
Hope it is helpul for you !【希望有所帮助】