有时候我们需要在代码中动态生成一个sqllite数据库文件,可以按照以下代码完成,

 1 static void Main(string[] args)
 2 {
 3 string connString = string.Format("Data Source={0}", "test.db");
4 MyContext context = new MyContext(new DbContextOptionsBuilder<MyContext>().UseSqlite(connString).Options); 5 6 7 context.Database.GetDbConnection().ConnectionString = connString; 8 context.Database.ExecuteSqlCommand(new RawSqlString(@"CREATE TABLE TagNames ( 9 Id INTEGER NOT NULL 10 CONSTRAINT PK_TagNames PRIMARY KEY AUTOINCREMENT, 11 Name TEXT NOT NULL, 12 Value TEXT 13 ); 14 15 ")); 16 //TagName是我定义的一个表Context 17 context.TagNames.Add(new TagName() { Name = "new tag" }); 18 context.SaveChanges(); 19 Console.WriteLine("complete"); 20 }