ef 实体框架入门教程:快速掌握数据库操作与代码优先迁移

dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools

public class Product {

public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int StockQuantity { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.Now;

}

using var context = new AppDbContext();

var newProduct = new Product {

ef 实体框架入门教程:快速掌握数据库操作与代码优先迁移

Name = "无线蓝牙耳机",
Price = 299.99m,
StockQuantity = 50

};

context.Products.Add(newProduct); await context.SaveChangesAsync();

public class User {

public int UserId { get; set; }
public string Username { get; set; }

// 导航属性
public UserProfile Profile { get; set; }

}

public class UserProfile {

ef 实体框架入门教程:快速掌握数据库操作与代码优先迁移

public int UserProfileId { get; set; }
public string FullName { get; set; }
public DateTime? BirthDate { get; set; }

// 外键属性
public int UserId { get; set; }

// 导航属性
public User User { get; set; }

}

dotnet ef migrations add InitialCreate

var orders = context.Orders.ToList(); foreach (var order in orders) {

var customer = context.Customers.Find(order.CustomerId);
// 每次Find都是一次数据库查询

}

你可能想看:
免责声明:本网站部分内容由用户自行上传,若侵犯了您的权益,请联系我们处理,谢谢!联系QQ:2760375052

分享:

扫一扫在手机阅读、分享本文

最近发表