最终实现后达到的效果,只需写少量代码就可实现CURD操作。
DAL层代码:

public class StudentDAL
{
EntityManager entityManager = EntityManagerFactory.CreateEntityManager();
public StudentDAL() { }
public StudentDAL(IDbTransaction transaction)
{
entityManager.Transaction = transaction;
}
public List<StudentEntity> FindAll()
{
return entityManager.FindAll<StudentEntity>();
}
public int Save(StudentEntity entity)
{
return entityManager.Save(entity);
}
public int Update(StudentEntity entity)
{
return entityManager.Update(entity);
}
public int Remove(StudentEntity entity)
{
return entityManager.Remove(entity);
}
public int Remove(object id)
{
return entityManager.Remove<StudentEntity>(id);
}
public List<StudentEntity> FindById(object id)
{
return entityManager.FindById<StudentEntity>(id);
}
public List<StudentEntity> FindByProperty(string propertyName,object value)
{
return entityManager.FindByProperty<StudentEntity>(propertyName, value);
}
}
{
EntityManager entityManager = EntityManagerFactory.CreateEntityManager();
public StudentDAL() { }
public StudentDAL(IDbTransaction transaction)
{
entityManager.Transaction = transaction;
}
public List<StudentEntity> FindAll()
{
return entityManager.FindAll<StudentEntity>();
}
public int Save(StudentEntity entity)
{
return entityManager.Save(entity);
}
public int Update(StudentEntity entity)
{
return entityManager.Update(entity);
}
public int Remove(StudentEntity entity)
{
return entityManager.Remove(entity);
}
public int Remove(object id)
{
return entityManager.Remove<StudentEntity>(id);
}
public List<StudentEntity> FindById(object id)
{
return entityManager.FindById<StudentEntity>(id);
}
public List<StudentEntity> FindByProperty(string propertyName,object value)
{
return entityManager.FindByProperty<StudentEntity>(propertyName, value);
}
}
实体类与数据库表的映射关系配置:

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Orm.CustomAttributes;
namespace Entity
{
[Serializable]
[Table(name="Student")]
public class StudentEntity
{
private string stuid;
private string stuno;
private string name;
private int sex;
private int age;
private string address;
private string telphone;
[Id(Name=”stuid”,Strategy = GenerationType.SEQUENCE)]
public string Stuid
{
get { return stuid; }
set { stuid = value; }
}
[Column(Name = "studentno")]
public string Stuno
{
get { return stuno; }
set { stuno = value; }
}
[Column(IsInsert = true)]
public string Name
{
get { return name; }
set { name = value; }
}
[Column(IsUpdate = true)]
public int Sex
{
get { return sex; }
set { sex = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string Telphone
{
get { return telphone; }
set { telphone = value; }
}
}
}
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Orm.CustomAttributes;
namespace Entity
{
[Serializable]
[Table(name="Student")]
public class StudentEntity
{
private string stuid;
private string stuno;
private string name;
private int sex;
private int age;
private string address;
private string telphone;
[Id(Name=”stuid”,Strategy = GenerationType.SEQUENCE)]
public string Stuid
{
get { return stuid; }
set { stuid = value; }
}
[Column(Name = "studentno")]
public string Stuno
{
get { return stuno; }
set { stuno = value; }
}
[Column(IsInsert = true)]
public string Name
{
get { return name; }
set { name = value; }
}
[Column(IsUpdate = true)]
public int Sex
{
get { return sex; }
set { sex = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string Telphone
{
get { return telphone; }
set { telphone = value; }
}
}
}
BLL层代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL;
using Entity;
using System.Orm.DBTransaction;
namespace BLL
{
public class StudentBP
{
public List<StudentEntity> FindAll()
{
StudentDAL dal = new StudentDAL();
return dal.FindAll();
}
public void Save(StudentEntity entity)
{
IDbTransaction trans = null;
try
{
trans = TransactionManager.CreateTransaction();
StudentDAL dal = new StudentDAL(trans);
dal.Save(entity);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Dispose();
}
}
public void Remove(object id)
{
IDbTransaction trans = null;
try
{
trans = TransactionManager.CreateTransaction();
StudentDAL dal = new StudentDAL(trans);
dal.Remove(id);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally {
trans.Dispose();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL;
using Entity;
using System.Orm.DBTransaction;
namespace BLL
{
public class StudentBP
{
public List<StudentEntity> FindAll()
{
StudentDAL dal = new StudentDAL();
return dal.FindAll();
}
public void Save(StudentEntity entity)
{
IDbTransaction trans = null;
try
{
trans = TransactionManager.CreateTransaction();
StudentDAL dal = new StudentDAL(trans);
dal.Save(entity);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Dispose();
}
}
public void Remove(object id)
{
IDbTransaction trans = null;
try
{
trans = TransactionManager.CreateTransaction();
StudentDAL dal = new StudentDAL(trans);
dal.Remove(id);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally {
trans.Dispose();
}
}
}
}
在实体类中配置[Table(Name="Student")],对应数据库中的表名:Student
在实体类中配置[Id(Name=”studentid”,Strategy = GenerationType.SEQUENCE)],表示当前属性是Student表中的主键ID,Name=”studentid”表示该属性Stuid对应Student表列studentid,Strategy表示主键生成策略,这里是自动增长。
在实体类中配置[Column(Name="studentno")],表示当前属性Stuno对应Student表中的列名:studentno(默认属性名=列名)
在实体类中配置[Column(IsInsert=false)],表示当前列值不插入到数据库(默认插入)
在实体类中配置[Column(IsUpdate=false)],表示当前列值不更新到数据库(默认更新)
(实体类映射配置和一些命名参考了JAVA中的JPA)
在下一篇中将开始研究如何一步一步的构建一个简单的ORM框架。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构