.NET Core下有很多热门的ORM框架,以下是其中六个常用的框架,包括EF Core、DApper、NHibernate、Fluent NHibernate、LLBLGen Pro和PetaPoco。接下来,我将为您详细介绍每个框架的优缺点,并提供示例代码演示如何使用。
示例代码:
// 定义模型类
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();
}
示例代码:
// 查询数据
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);
}
示例代码:
// 定义映射文件
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();
}
}
示例代码:
// 定义映射类
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();
}
}
示例代码:
// 定义模型类
[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);
}
示例代码:
// 查询数据
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等。