其他分享
首页 > 其他分享> > BaseEntity

BaseEntity

作者:互联网

// See https://aka.ms/new-console-template for more information
using FreeSql;
using FreeSql.DataAnnotations;
using System.Diagnostics;

Console.WriteLine("Hello, World!");

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, @"Data Source=121.5.123.250;Port=3306;User ID=jyds_lis123;Password=jhh6JkD33e5FBEnP; Initial Catalog=jyds_lis;Charset=utf8; SslMode=none;Min pool size=1")
    .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
    .Build();

//Lazy<IFreeSql> fsql = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
//        .UseMonitorCommand(cmd => Trace.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句,Trace在输入选项卡中查看
//        .UseConnectionString(FreeSql.DataType.MySql, @"Data Source=127.0.0.1;Port=3306;User ID=jyds_lis123;Password=jhh6JkD33e5FBEnP; Initial Catalog=jyds_lis;Charset=utf8; SslMode=none;Min pool size=1")
//        .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
//        .Build());

AsyncLocal<IUnitOfWork> _asyncUow = new AsyncLocal<IUnitOfWork>();

BaseEntity.Initialization(fsql, () => _asyncUow.Value);

//添加
var item = new UserGroup { GroupName = "组一" };

item.Insert();

//更新
item.GroupName = "组二";
item.Update();

//添加或更新
item.Save();

//软删除
item.Delete();

//恢复软删除
item.Restore();

//根据主键获取对象
item = UserGroup.Find(1);

//查询数据
var items = UserGroup.Where(a => a.Id > 10).ToList();

Console.WriteLine(items.Count);


public class UserGroup : BaseEntity<UserGroup, int>
{
    public string GroupName { get; set; }
}

public class UserGroup2 : BaseEntity<UserGroup, int>
{
    [Column(IsIdentity = false)]
    public override int Id { get; set; }
    public string GroupName { get; set; }
}


public class User : BaseEntity<UserGroup, Guid>
{
    public string UserName { get; set; }
}



标签:get,FreeSql,item,BaseEntity,new,public
来源: https://www.cnblogs.com/renzhe-org/p/16683897.html