public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) { Configuration = configuration; Fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10") .UseAutoSyncStructure(true) .Build(); //Fsql.CodeFirst.EfCoreFluentApiTestGeneric(); Fsql.CodeFirst.EfCoreFluentApiTestDynamic(); BaseDBContext.Fsql = Fsql; var sql11 = Fsql.Select <Topic1>().ToSql(); //SELECT a."Id", a."Title", a."CreateTime" FROM "Topic1" a var sql12 = Fsql.Insert <Topic1>().AppendData(new Topic1()).ToSql(); //INSERT INTO "Topic1"("Id", "Title", "CreateTime") VALUES(@Id0, @Title0, @CreateTime0) var sql21 = Fsql.Select <Topic2>().ToSql(); //SELECT a."Id", a."Title", a."CreateTime" FROM "Topic2" a var sql22 = Fsql.Insert <Topic2>().AppendData(new Topic2()).ToSql(); //INSERT INTO "Topic2"("Id", "Title", "CreateTime") VALUES(@Id0, @Title0, @CreateTime0) using (var db = new Topic1Context()) { db.Topic1s.Add(new Topic1()); } using (var db = new Topic2Context()) { db.Topic2s.Add(new Topic2()); } var sql13 = Fsql.Select <Topic1>().ToSql(); //SELECT a."topic1_id", a."Title", a."CreateTime" FROM "topic1_sss" a var sql14 = Fsql.Insert <Topic1>().AppendData(new Topic1()).ToSql(); //INSERT INTO "topic1_sss"("Title", "CreateTime") VALUES(@Title0, @CreateTime0) var sql23 = Fsql.Select <Topic2>().ToSql(); //SELECT a."topic2_id", a."Title", a."CreateTime" FROM "topic2_sss" a var sql24 = Fsql.Insert <Topic2>().AppendData(new Topic2()).ToSql(); //INSERT INTO "topic2_sss"("Title", "CreateTime") VALUES(@Title0, @CreateTime0) }
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) { Configuration = configuration; Fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10") .UseLogger(loggerFactory.CreateLogger <IFreeSql>()) .UseAutoSyncStructure(true) .UseLazyLoading(true) .UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText)) .Build(); var sysu = new SysUser { }; Fsql.Insert <SysUser>().AppendData(sysu).ExecuteAffrows(); Fsql.Insert <SysUserLogOn>().AppendData(new SysUserLogOn { UserLogOnId = sysu.UserId }).ExecuteAffrows(); var a = Fsql.Select <SysUserLogOn>().ToList(); var b = Fsql.Select <SysUserLogOn>().Any(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, "Data Source=document.db; Pooling=true;Min Pool Size=1") //.UseConnectionString(FreeSql.DataType.MsAccess, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=document.mdb") //如果提示Microsoft.ACE.OLEDB.12.0未注册下载安装 https://download.microsoft.com/download/E/4/2/E4220252-5FAE-4F0A-B1B9-0B48B5FBCCF9/AccessDatabaseEngine_X64.exe .UseAutoSyncStructure(true) //自动同步实体结构【开发环境必备】 .UseMonitorCommand(cmd => Console.Write(cmd.CommandText)) .Build(); var r = new Random(); var ItemList = new List <Item>() { new Item { Text = "假装 First item", Description = "This is an item description." }, new Item { Text = "的哥 Second item", Description = "This is an item description." }, new Item { Text = "四风 Third item", Description = "This is an item description." }, new Item { Text = "加州 Fourth item", Description = "This is an item description." }, new Item { Text = "阳光 Fifth item", Description = "This is an item description." }, new Item { Text = "孔雀 Sixth item - " + r.Next(11000).ToString(), Description = "This is an item description." } }; if (fsql.Select <Item>().Count() < 100) { fsql.Insert <Item>().AppendData(ItemList).ExecuteAffrows(); } ItemList = fsql.Select <Item>().ToList(); Console.WriteLine("\r\n\r\nItemListCount: " + ItemList.Count()); Console.WriteLine("\r\n\r\nLastItem: " + ItemList.Last().Text); }
public void WithLambdaParameter01() { using (var fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=6") .UseAutoSyncStructure(true) .UseGenerateCommandParameterWithLambda(true) .UseMonitorCommand( cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText) //监听SQL命令对象,在执行前 //, (cmd, traceLog) => Console.WriteLine(traceLog) ) .UseLazyLoading(true) .Build()) { fsql.Delete <ts_wlp01>().Where("1=1").ExecuteAffrows(); var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); var affrows = fsql.Insert(new[] { new ts_wlp01 { id = id1, status = ts_wlp01_status.completed, }, new ts_wlp01 { id = id2, status = ts_wlp01_status.pending, } }).ExecuteAffrows(); Assert.Equal(2, affrows); var items = fsql.Select <ts_wlp01>().OrderBy(a => a.status).ToList(); Assert.Equal(2, items.Count); Assert.Equal(id1, items[0].id); Assert.Equal(ts_wlp01_status.completed, items[0].status); Assert.Equal(id2, items[1].id); Assert.Equal(ts_wlp01_status.pending, items[1].status); var item1Select = fsql.Select <ts_wlp01>().Where(a => a.status == ts_wlp01_status.completed); var item1S0p = item1Select as Select0Provider; Assert.Single(item1S0p._params); var p0 = item1S0p._params[0]; Assert.Equal(p0.DbType, System.Data.DbType.String); Assert.Equal(p0.Value, "completed"); items = item1Select.ToList(); Assert.Single(items); Assert.Equal(id1, items[0].id); Assert.Equal(ts_wlp01_status.completed, items[0].status); var item2Select = fsql.Select <ts_wlp01>().Where(a => a.status == ts_wlp01_status.pending); var item2S0p = item2Select as Select0Provider; Assert.Single(item2S0p._params); p0 = item2S0p._params[0]; Assert.Equal(p0.DbType, System.Data.DbType.String); Assert.Equal(p0.Value, "pending"); items = item2Select.ToList(); Assert.Single(items); Assert.Equal(id2, items[0].id); Assert.Equal(ts_wlp01_status.pending, items[0].status); // use var var item1status = ts_wlp01_status.completed; item1Select = fsql.Select <ts_wlp01>().Where(a => a.status == item1status); item1S0p = item1Select as Select0Provider; Assert.Single(item1S0p._params); p0 = item1S0p._params[0]; Assert.Equal(p0.DbType, System.Data.DbType.String); Assert.Equal(p0.Value, "completed"); items = item1Select.ToList(); Assert.Single(items); Assert.Equal(id1, items[0].id); Assert.Equal(item1status, items[0].status); var item2status = ts_wlp01_status.pending; item2Select = fsql.Select <ts_wlp01>().Where(a => a.status == item2status); item2S0p = item2Select as Select0Provider; Assert.Single(item2S0p._params); p0 = item2S0p._params[0]; Assert.Equal(p0.DbType, System.Data.DbType.String); Assert.Equal(p0.Value, "pending"); items = item2Select.ToList(); Assert.Single(items); Assert.Equal(id2, items[0].id); Assert.Equal(item2status, items[0].status); } }