特性git
如何开始?github
string connectionString = "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=your db;Pooling=true;Maximum Pool Size=100"; ILogger logger = loggerFactory.CreateLogger<MyStaging.Helpers.PgSqlHelper>(); _startup.Init(logger, connectionString);
实体对象说明
构建工具会自动生成DAL层,包含实体模型和对象关系,因为执行数据库查询的过程当中,高度依赖实体模型映射,因此在生成实体模型时,对实体作了一些ORM的映射设置,这些设置对实体模型的影响很是小。sql
EntityMappingAttribute
该特性类接受一个属性:TableName,指明该实体模型映射到数据库中的>模式.表名,如数据库
``` [EntityMapping(TableName = "public.user")] public partial class Public_userModel { } ```
ForeignKeyMappingAttribute
应用该该特性类到属性上,表示这个是一个外键引用的属性,如json
``` private Public_userModel _public_User=null; [ForeignKeyMapping,JsonIgnore]public Public_userModel Public_User { get{ if(_public_User==null)_public_User= Public_user.Context.Where(f=>f.Id==this.User_id).ToOne(); return _public_User;} } ``` *以上代码还应用了特性:JsonIgnore ,表示,该外键在对象进行 json 序列化的时候选择忽略该属性*
NonDbColumnMappingAttribute
应用该该特性类到属性上,表示这个是一个自定义的属性,在进行数据库查询的时候将忽略该属性,如c#
``` [NonDbColumnMappingAttribute,JsonIgnore] public Public_user.UpdateBuilder UpdateBuilder{get{return new Public_user.UpdateBuilder(this.Id);}} ```
以上代码还应用了特性:JsonIgnore ,表示,该外键在对象进行 json 序列化的时候选择忽略该属性服务器
MyStaging.Helpers.QueryContext
DAL继承的基类,该类实现了全部对数据库操做的封装,可直接继承使用,若是使用脚手架附带的构建工具,直接进行业务编写便可app
数据库操做框架
Public_userModel user = new Public_userModel(); user.Id = Guid.NewGuid(); user.Login_name = "test@gmail.com"; Public_user.Insert(user);
// 自动根据主键修改 Public_userModel user = new Public_userModel(); user.UpdateBuilder.SetLogin_time(DateTime.Now).SaveChange(); // 自定义条件修改 user.UpdateBuilder.SetLogin_time(DateTime.Now).Where(f => f.Sex == true).SaveChange(); // 直接修改 Public_user.Update(Guid.Empty).SetLogin_time(DateTime.Now).Where(f => f.Sex == true).SaveChange(); // 自定义条件的直接修改 Public_user.UpdateBuilder.SetLogin_time(DateTime.Now).Where(f => f.Id == Guid.Empty).Where(f => f.Sex == true).SaveChange();
// 根据主键删除 Public_user.Delete(Guid.Empty); // 根据条件删除 Public_user.DeleteBuilder.Where(f => f.Id == Guid.Empty).Where(f => f.Sex == true).SaveChange();
Public_userModel user = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne();
Public_userModel user = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne("id","login_name");
public class UserModel{ public string Login_name{get;set;} public Guid Id{get;set;} } Public_userModel user = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne<UserModel>("id","login_name");
List<Public_userModel> list = Public_user.Context.ToList(); List<Public_userModel> list = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToList(); public class UserModel{ public string Login_name{get;set;} public Guid Id{get;set;} } List<UserModel> list = Public_user.Context.ToList<UserModel>("id","login_name");
public class UserModel{ public string Login_name{get;set;} public Guid Id{get;set;} } List<UserModel> list = Public_user.Context.Union<TopicModel>("b",UnionType.INNER_JOIN,(a,b)=>a.Id==b.User_Id).Where(a=>a.Id=Guid.Empty).Where<TopicModel>(b=>b.Publish==true).ToList<UserModel>("id","login_name");
Public_user.Context.Where(f => f.Login_name == "test@gmail.com").OrderBy(f=>f.State).Page(1,10);
Public_user.Context.Where(f => f.Login_name == "test@gmail.com").OrderBy(f=>f.State); Public_user.Context.Where(f => f.Login_name == "test@gmail.com").OrderDescing(f=>f.State);
Public_user.Context.Where(f => f.Login_name == "test@gmail.com").Avg(f=>f.Age); Public_user.Context.Where(f => f.Login_name == "test@gmail.com").Sum(f=>f.Blance); // Max,Min,GroupBy,Having
PgSqlHelper.Transaction(() => { Public_userModel user= Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne(); user.UpdateBuilder.SetLogin_time(DateTime.Now).SaveChange(); });