<返回更多

.NET Core下有热门的ORM框架使用方法

2023-11-16    
加入收藏

.NET Core下有很多热门的ORM框架,以下是其中六个常用的框架,包括EF Core、DApper、NHibernate、Fluent NHibernate、LLBLGen Pro和PetaPoco。接下来,我将为您详细介绍每个框架的优缺点,并提供示例代码演示如何使用。

  1. Entity Framework Core (EF Core)

示例代码:

// 定义模型类
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// 创建DbContext
public class MyDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connectionString");
}
}
// 查询数据
using (var dbContext = new MyDbContext())
{
var products = dbContext.Products.Where(p => p.Price > 10).ToList();
}
// 插入数据
using (var dbContext = new MyDbContext())
{
var newProduct = new Product { Name = "New Product", Price = 20 };
dbContext.Products.Add(newProduct);
dbContext.SaveChanges();
}
  1. Dapper

示例代码:

// 查询数据
using (var connection = new SqlConnection("connectionString"))
{
var products = connection.Query<Product>("SELECT * FROM Products WHERE Price > @Price", new { Price = 10 }).ToList();
}
// 插入数据
using (var connection = new SqlConnection("connectionString"))
{
var newProduct = new Product { Name = "New Product", Price = 20 };
connection.Execute("INSERT INTO Products (Name, Price) VALUES (@Name, @Price)", newProduct);
}
  1. NHibernate

示例代码:

// 定义映射文件
public class ProductMap : ClassMapping<Product>
{
public ProductMap()
{
Id(x => x.Id, map => map.Generator(Generators.Identity));
Property(x => x.Name);
Property(x => x.Price);
Table("Products");
}
}
// 创建SessionFactory
var configuration = new Configuration();
configuration.Configure(); // 加载配置文件
configuration.AddMapping(typeof(ProductMap)); // 添加映射文件
var sessionFactory = configuration.BuildSessionFactory();
// 查询数据
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var products = session.Query<Product>().Where(p => p.Price > 10).ToList();
transaction.Commit();
}
}
// 插入数据
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var newProduct = new Product { Name = "New Product", Price = 20 };
session.Save(newProduct);
transaction.Commit();
}
}
  1. Fluent NHibernate

示例代码:

// 定义映射类
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Price);
Table("Products");
}
}
// 创建SessionFactory
var sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012.ConnectionString("connectionString"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProductMap>())
.BuildSessionFactory();
// 查询数据
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var products = session.Query<Product>().Where(p => p.Price > 10).ToList();
transaction.Commit();
}
}
// 插入数据
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var newProduct = new Product { Name = "New Product", Price = 20 };
session.Save(newProduct);
transaction.Commit();
}
}
  1. LLBLGen Pro

示例代码:

// 定义模型类
[Serializable]
[DataEntity(IsGenerated = true)]
public partial class Product : EntityBase
{
[DataField(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[DataField]
public string Name { get; set; }
[DataField]
public decimal Price { get; set; }
}
// 查询数据
using (var adapter = new DataAccessAdapter())
{
var products = adapter.FetchQuery<Product>(new RelationPredicateBucket(ProductFields.Price > 10));
}
// 插入数据
using (var adapter = new DataAccessAdapter())
{
var newProduct = new Product { Name = "New Product", Price = 20 };
adapter.SaveEntity(newProduct);
}
  1. PetaPoco

示例代码:

// 查询数据
using (var db = new Database("connectionString"))
{
		var products = db.Query<Product>("SELECT * FROM Products WHERE Price > @0", 10);
}
// 插入数据
using (var db = new Database("connectionString"))
{
    var newProduct = new Product { Name = "New Product", Price = 20 };
    db.Insert(newProduct);
}

以上是几种常见的.NET ORM框架,每种框架都有其特点和适用场景。选择合适的框架需要根据项目需求、团队经验和个人偏好来决定。如果需要更高级的功能和更好的性能,可以考虑使用商业级的ORM框架,如Entity Framework Core、LLBLGen Pro等。如果对性能要求较高,可以考虑使用轻量级的框架,如Dapper、PetaPoco等。

关键词:ORM框架      点击(20)
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多ORM框架相关>>>