diff --git a/SQLiteEFCodeFirstDemo.sln b/SQLiteEFCodeFirstDemo.sln new file mode 100644 index 0000000..23507b9 --- /dev/null +++ b/SQLiteEFCodeFirstDemo.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30406.217 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLiteEFCodeFirstDemo", "SQLiteEFCodeFirstDemo\SQLiteEFCodeFirstDemo.csproj", "{A8A315F7-36C3-4812-9F09-EF13582E7F68}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A8A315F7-36C3-4812-9F09-EF13582E7F68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A8A315F7-36C3-4812-9F09-EF13582E7F68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A8A315F7-36C3-4812-9F09-EF13582E7F68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A8A315F7-36C3-4812-9F09-EF13582E7F68}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {53D098E6-8F56-4CED-8299-B9406CC0A257} + EndGlobalSection +EndGlobal diff --git a/SQLiteEFCodeFirstDemo/App.config b/SQLiteEFCodeFirstDemo/App.config new file mode 100644 index 0000000..7e2b71f --- /dev/null +++ b/SQLiteEFCodeFirstDemo/App.config @@ -0,0 +1,29 @@ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/Configuration.cs b/SQLiteEFCodeFirstDemo/Configuration.cs new file mode 100644 index 0000000..d00e524 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Configuration.cs @@ -0,0 +1,39 @@ +using System.Data.Entity.Migrations; +using System.Data.SQLite.EF6.Migrations; + +namespace SQLiteEFCodeFirstDemo +{ + public class Configuration : DbMigrationsConfiguration + { + public Configuration() + { + AutomaticMigrationsEnabled = true; + AutomaticMigrationDataLossAllowed = true; + SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator()); + } + + protected override void Seed(MyDatabase context) + { + context.Users.AddOrUpdate( + new Models.User { ID = 1, Name = "张三", Age = 22, Gender = "男", Location = "北京"}, + new Models.User { ID = 2, Name = "李四", Age = 23, Gender = "男", Location = "上海"}, + new Models.User { ID = 3, Name = "王五", Age = 30, Gender = "男", Location = "广州"}, + new Models.User { ID = 4, Name = "赵六", Age = 27, Gender = "男", Location = "安徽"}, + new Models.User { ID = 5, Name = "张丽", Age = 19, Gender = "女", Location = "深圳"} + ); + + // This method will be called after migrating to the latest version. + + // You can use the DbSet.AddOrUpdate() helper extension method + // to avoid creating duplicate seed data. E.g. + // + // context.People.AddOrUpdate( + // p => p.FullName, + // new Person { FullName = "Andrew Peters" }, + // new Person { FullName = "Brice Lambson" }, + // new Person { FullName = "Rowan Miller" } + // ); + // + } + } +} \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/FormMain.Designer.cs b/SQLiteEFCodeFirstDemo/FormMain.Designer.cs new file mode 100644 index 0000000..6de4be0 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/FormMain.Designer.cs @@ -0,0 +1,298 @@ +namespace SQLiteEFCodeFirstDemo +{ + partial class FormMain + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.BtnCreate = new System.Windows.Forms.Button(); + this.BtnRefresh = new System.Windows.Forms.Button(); + this.BtnUpdate = new System.Windows.Forms.Button(); + this.BtnDelete = new System.Windows.Forms.Button(); + this.LblText = new System.Windows.Forms.Label(); + this.TxtID = new System.Windows.Forms.TextBox(); + this.LblID = new System.Windows.Forms.Label(); + this.BtnQuery = new System.Windows.Forms.Button(); + this.TxtKeyword = new System.Windows.Forms.TextBox(); + this.LblKeyword = new System.Windows.Forms.Label(); + this.BtnSearch = new System.Windows.Forms.Button(); + this.LblName = new System.Windows.Forms.Label(); + this.TxtName = new System.Windows.Forms.TextBox(); + this.CmbGender = new System.Windows.Forms.ComboBox(); + this.LblGender = new System.Windows.Forms.Label(); + this.LblAge = new System.Windows.Forms.Label(); + this.TxtAge = new System.Windows.Forms.TextBox(); + this.LblLocation = new System.Windows.Forms.Label(); + this.TxtLocation = new System.Windows.Forms.TextBox(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView1 + // + this.dataGridView1.AllowUserToAddRows = false; + this.dataGridView1.AllowUserToDeleteRows = false; + this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; + this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView1.Location = new System.Drawing.Point(218, 64); + this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.ReadOnly = true; + this.dataGridView1.RowTemplate.Height = 23; + this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dataGridView1.Size = new System.Drawing.Size(474, 259); + this.dataGridView1.TabIndex = 0; + this.dataGridView1.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_RowEnter); + // + // BtnCreate + // + this.BtnCreate.Location = new System.Drawing.Point(24, 299); + this.BtnCreate.Name = "BtnCreate"; + this.BtnCreate.Size = new System.Drawing.Size(50, 23); + this.BtnCreate.TabIndex = 9; + this.BtnCreate.Text = "创建"; + this.BtnCreate.UseVisualStyleBackColor = true; + this.BtnCreate.Click += new System.EventHandler(this.BtnCreate_Click); + // + // BtnRefresh + // + this.BtnRefresh.Location = new System.Drawing.Point(617, 22); + this.BtnRefresh.Name = "BtnRefresh"; + this.BtnRefresh.Size = new System.Drawing.Size(75, 23); + this.BtnRefresh.TabIndex = 12; + this.BtnRefresh.Text = "刷新"; + this.BtnRefresh.UseVisualStyleBackColor = true; + this.BtnRefresh.Click += new System.EventHandler(this.BtnRefresh_Click); + // + // BtnUpdate + // + this.BtnUpdate.Location = new System.Drawing.Point(83, 299); + this.BtnUpdate.Name = "BtnUpdate"; + this.BtnUpdate.Size = new System.Drawing.Size(50, 23); + this.BtnUpdate.TabIndex = 10; + this.BtnUpdate.Text = "更新"; + this.BtnUpdate.UseVisualStyleBackColor = true; + this.BtnUpdate.Click += new System.EventHandler(this.BtnUpdate_Click); + // + // BtnDelete + // + this.BtnDelete.Location = new System.Drawing.Point(142, 299); + this.BtnDelete.Name = "BtnDelete"; + this.BtnDelete.Size = new System.Drawing.Size(50, 23); + this.BtnDelete.TabIndex = 11; + this.BtnDelete.Text = "删除"; + this.BtnDelete.UseVisualStyleBackColor = true; + this.BtnDelete.Click += new System.EventHandler(this.BtnDelete_Click); + // + // LblText + // + this.LblText.AutoSize = true; + this.LblText.Location = new System.Drawing.Point(216, 327); + this.LblText.Name = "LblText"; + this.LblText.Size = new System.Drawing.Size(0, 12); + this.LblText.TabIndex = 0; + // + // TxtID + // + this.TxtID.Location = new System.Drawing.Point(49, 23); + this.TxtID.Name = "TxtID"; + this.TxtID.Size = new System.Drawing.Size(63, 21); + this.TxtID.TabIndex = 1; + // + // LblID + // + this.LblID.AutoSize = true; + this.LblID.Location = new System.Drawing.Point(22, 27); + this.LblID.Name = "LblID"; + this.LblID.Size = new System.Drawing.Size(23, 12); + this.LblID.TabIndex = 0; + this.LblID.Text = "ID:"; + // + // BtnQuery + // + this.BtnQuery.Location = new System.Drawing.Point(118, 22); + this.BtnQuery.Name = "BtnQuery"; + this.BtnQuery.Size = new System.Drawing.Size(75, 23); + this.BtnQuery.TabIndex = 2; + this.BtnQuery.Text = "查找"; + this.BtnQuery.UseVisualStyleBackColor = true; + this.BtnQuery.Click += new System.EventHandler(this.BtnQuery_Click); + // + // TxtKeyword + // + this.TxtKeyword.Location = new System.Drawing.Point(269, 23); + this.TxtKeyword.Name = "TxtKeyword"; + this.TxtKeyword.Size = new System.Drawing.Size(100, 21); + this.TxtKeyword.TabIndex = 3; + // + // LblKeyword + // + this.LblKeyword.AutoSize = true; + this.LblKeyword.Location = new System.Drawing.Point(216, 27); + this.LblKeyword.Name = "LblKeyword"; + this.LblKeyword.Size = new System.Drawing.Size(47, 12); + this.LblKeyword.TabIndex = 0; + this.LblKeyword.Text = "关键字:"; + // + // BtnSearch + // + this.BtnSearch.Location = new System.Drawing.Point(375, 22); + this.BtnSearch.Name = "BtnSearch"; + this.BtnSearch.Size = new System.Drawing.Size(75, 23); + this.BtnSearch.TabIndex = 4; + this.BtnSearch.Text = "搜索"; + this.BtnSearch.UseVisualStyleBackColor = true; + this.BtnSearch.Click += new System.EventHandler(this.BtnSearch_Click); + // + // LblName + // + this.LblName.AutoSize = true; + this.LblName.Location = new System.Drawing.Point(21, 120); + this.LblName.Name = "LblName"; + this.LblName.Size = new System.Drawing.Size(35, 12); + this.LblName.TabIndex = 0; + this.LblName.Text = "姓名:"; + // + // TxtName + // + this.TxtName.Location = new System.Drawing.Point(62, 117); + this.TxtName.Name = "TxtName"; + this.TxtName.Size = new System.Drawing.Size(130, 21); + this.TxtName.TabIndex = 5; + // + // CmbGender + // + this.CmbGender.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.CmbGender.FormattingEnabled = true; + this.CmbGender.Items.AddRange(new object[] { + "男", + "女"}); + this.CmbGender.Location = new System.Drawing.Point(62, 158); + this.CmbGender.Name = "CmbGender"; + this.CmbGender.Size = new System.Drawing.Size(130, 20); + this.CmbGender.TabIndex = 6; + // + // LblGender + // + this.LblGender.AutoSize = true; + this.LblGender.Location = new System.Drawing.Point(21, 160); + this.LblGender.Name = "LblGender"; + this.LblGender.Size = new System.Drawing.Size(35, 12); + this.LblGender.TabIndex = 0; + this.LblGender.Text = "性别:"; + // + // LblAge + // + this.LblAge.AutoSize = true; + this.LblAge.Location = new System.Drawing.Point(21, 203); + this.LblAge.Name = "LblAge"; + this.LblAge.Size = new System.Drawing.Size(35, 12); + this.LblAge.TabIndex = 0; + this.LblAge.Text = "年龄:"; + // + // TxtAge + // + this.TxtAge.Location = new System.Drawing.Point(62, 198); + this.TxtAge.Name = "TxtAge"; + this.TxtAge.Size = new System.Drawing.Size(130, 21); + this.TxtAge.TabIndex = 7; + // + // LblLocation + // + this.LblLocation.AutoSize = true; + this.LblLocation.Location = new System.Drawing.Point(21, 244); + this.LblLocation.Name = "LblLocation"; + this.LblLocation.Size = new System.Drawing.Size(35, 12); + this.LblLocation.TabIndex = 0; + this.LblLocation.Text = "地区:"; + // + // TxtLocation + // + this.TxtLocation.Location = new System.Drawing.Point(62, 239); + this.TxtLocation.Name = "TxtLocation"; + this.TxtLocation.Size = new System.Drawing.Size(130, 21); + this.TxtLocation.TabIndex = 8; + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.BackColor = System.Drawing.Color.White; + this.ClientSize = new System.Drawing.Size(716, 348); + this.Controls.Add(this.CmbGender); + this.Controls.Add(this.TxtLocation); + this.Controls.Add(this.TxtAge); + this.Controls.Add(this.LblLocation); + this.Controls.Add(this.TxtName); + this.Controls.Add(this.LblAge); + this.Controls.Add(this.LblGender); + this.Controls.Add(this.LblName); + this.Controls.Add(this.BtnSearch); + this.Controls.Add(this.BtnQuery); + this.Controls.Add(this.LblKeyword); + this.Controls.Add(this.LblID); + this.Controls.Add(this.TxtKeyword); + this.Controls.Add(this.TxtID); + this.Controls.Add(this.LblText); + this.Controls.Add(this.BtnDelete); + this.Controls.Add(this.BtnUpdate); + this.Controls.Add(this.BtnRefresh); + this.Controls.Add(this.BtnCreate); + this.Controls.Add(this.dataGridView1); + this.Name = "FormMain"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "CodeFirst的SQLite EF示例程序"; + this.Load += new System.EventHandler(this.FormMain_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.DataGridView dataGridView1; + private System.Windows.Forms.Button BtnCreate; + private System.Windows.Forms.Button BtnRefresh; + private System.Windows.Forms.Button BtnUpdate; + private System.Windows.Forms.Button BtnDelete; + private System.Windows.Forms.Label LblText; + private System.Windows.Forms.TextBox TxtID; + private System.Windows.Forms.Label LblID; + private System.Windows.Forms.Button BtnQuery; + private System.Windows.Forms.TextBox TxtKeyword; + private System.Windows.Forms.Label LblKeyword; + private System.Windows.Forms.Button BtnSearch; + private System.Windows.Forms.Label LblName; + private System.Windows.Forms.TextBox TxtName; + private System.Windows.Forms.ComboBox CmbGender; + private System.Windows.Forms.Label LblGender; + private System.Windows.Forms.Label LblAge; + private System.Windows.Forms.TextBox TxtAge; + private System.Windows.Forms.Label LblLocation; + private System.Windows.Forms.TextBox TxtLocation; + } +} + diff --git a/SQLiteEFCodeFirstDemo/FormMain.cs b/SQLiteEFCodeFirstDemo/FormMain.cs new file mode 100644 index 0000000..2cef1ea --- /dev/null +++ b/SQLiteEFCodeFirstDemo/FormMain.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Windows.Forms; + +using SQLiteEFCodeFirstDemo.Models; + +namespace SQLiteEFCodeFirstDemo +{ + /********************************* + * CodeFirst模式下EF SQLite示例程序 + * + * 参考链接: + * + * C#/.NET VS2017+ EF+SQLite.CodeFirst——真正实现CodeFirst + * https://blog.csdn.net/qq_34202873/article/details/85068877 + * + * Sqlite:Code First模式 + * https://blog.csdn.net/wucdsg/article/details/78895366 + * + * SQLite 的 CodeFirst 模式 + * https://www.cnblogs.com/hippieZhou/archive/2018/08/04/9420432.html + * + * 以SQLite作为数据库的CodeFirst模式开发及踩的坑 + * https://blog.csdn.net/kindmb/article/details/102293986 + * + * SQLite在XP下出现no such table异常与找不到SQLite.Interop.dll故障 + * https://blog.csdn.net/kindmb/article/details/102328189 + * + * Sqlite && EF Code FIRST 终极解决方案 2019.5.17# + * https://www.cnblogs.com/swobble/p/10881756.html + * + * SQLite CodeFirst、Migration 的趟坑过程 + * https://blog.csdn.net/myinc/article/details/61953193 + * + * 感谢以上文章原作者 + * + ********************************/ + public partial class FormMain : Form + { + /// + /// 默认构造 + /// + public FormMain() + { + InitializeComponent(); + } + + + /// + /// 创建按钮事件 + /// + private void BtnCreate_Click(object sender, EventArgs e) + { + using (var db = new MyDatabase()) + { + db.Users.Add(GetInputUserInfo()); + int count = db.SaveChanges(); + LblText.Text = $"{DateTime.Now},插入{count}条记录"; + RefreshData(); + } + } + + + /// + /// 删除按钮事件 + /// + private void BtnDelete_Click(object sender, EventArgs e) + { + using (var db = new MyDatabase()) + { + var user = db.Users.FirstOrDefault(x => x.ID == int.Parse(TxtID.Text)); + if (user != null) + { + var result = db.Users.Remove(user); + int count = db.SaveChanges(); + LblText.Text = $"{DateTime.Now}, 删除{count}条记录"; + SetInputUserInfo(null); + RefreshData(); + } + } + } + + + /// + /// 查找按钮事件 + /// + private void BtnQuery_Click(object sender, EventArgs e) + { + using (var db = new MyDatabase()) + { + var user = db.Users.Find(int.Parse(TxtID.Text)); + if (user == null) + LblText.Text = $"{DateTime.Now}, 未找到指定用户"; + else + SetInputUserInfo(user); + } + } + + /// + /// 刷新按钮事件 + /// + private void BtnRefresh_Click(object sender, EventArgs e) + { + RefreshData(); + } + + /// + /// 搜索按钮事件 + /// + private void BtnSearch_Click(object sender, EventArgs e) + { + if (string.IsNullOrWhiteSpace(TxtKeyword.Text)) + return; + using (var db = new MyDatabase()) + { + var users = db.Users.Where(x => x.Name.Contains(TxtKeyword.Text)).ToList(); + LblText.Text = $"{DateTime.Now}, 查到{users.Count}条记录"; + dataGridView1.DataSource = users; + } + } + + /// + /// 更新按钮事件 + /// + private void BtnUpdate_Click(object sender, EventArgs e) + { + using (var db = new MyDatabase()) + { + var user = GetInputUserInfo(); + user.ID = int.Parse(TxtID.Text); + db.Users.Attach(user); + int count = db.SaveChanges(); + LblText.Text = $"{DateTime.Now}, 修改{count}条记录"; + RefreshData(); + } + } + + /// + /// 行选中时触发 + /// + private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) + { + if (e.RowIndex >= 0) + { + // 更新左侧输入框 + if (dataGridView1.DataSource is List users) + SetInputUserInfo(users[e.RowIndex]); + } + } + + /// + /// 获取输入框用户信息 + /// + /// + private User GetInputUserInfo() + { + return new User + { + Name = TxtName.Text, + Gender = CmbGender.Text, + Age = int.Parse(TxtAge.Text), + Location = TxtLocation.Text + }; + } + + /// + /// 刷新数据 + /// + private void RefreshData() + { + using (var db = new MyDatabase()) + { + var users = db.Users.ToList(); + dataGridView1.DataSource = users; + } + } + + /// + /// 设置输入框用户信息 + /// + /// + private void SetInputUserInfo(User user) + { + if (user != null) + { + TxtID.Text = user.ID.ToString(); + TxtName.Text = user.Name; + CmbGender.Text = user.Gender; + TxtAge.Text = user.Age.ToString(); + TxtLocation.Text = user.Location; + } + else + { + TxtID.Text = string.Empty; + TxtName.Text = string.Empty; + CmbGender.SelectedIndex = 0; + TxtAge.Text = string.Empty; + TxtLocation.Text = string.Empty; + } + } + + /// + /// 窗口加载事件 + /// + /// + /// + private void FormMain_Load(object sender, EventArgs e) + { + RefreshData(); + } + } +} \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/FormMain.resx b/SQLiteEFCodeFirstDemo/FormMain.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/FormMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/Models/User.cs b/SQLiteEFCodeFirstDemo/Models/User.cs new file mode 100644 index 0000000..133ac67 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Models/User.cs @@ -0,0 +1,22 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace SQLiteEFCodeFirstDemo.Models +{ + public class User + { + public int ID { get; set; } + + [DisplayName("姓名"), StringLength(20)] + public string Name { get; set; } + + [DisplayName("年龄")] + public int Age { get; set; } + + [DisplayName("性别"), StringLength(3)] + public string Gender { get; set; } + + [DisplayName("地区"), StringLength(30)] + public string Location { get; set; } + } +} \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/MyDatabase.cs b/SQLiteEFCodeFirstDemo/MyDatabase.cs new file mode 100644 index 0000000..12474f3 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/MyDatabase.cs @@ -0,0 +1,30 @@ +using System; +using System.Data.Entity; + +using SQLiteEFCodeFirstDemo.Models; + +namespace SQLiteEFCodeFirstDemo +{ + public class MyDatabase : DbContext + { + public MyDatabase() + : base("name=MyDatabase") + { + // 解决参数中使用相对路径导致无法创建数据库问题 + // https://blog.csdn.net/kindmb/article/details/102328189 + Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; + + // 设定初始化器为自动迁移数据库到最新版本 + Database.SetInitializer(new MigrateDatabaseToLatestVersion()); + } + + protected override void OnModelCreating(DbModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + // 自动发现 + //modelBuilder.Configurations.AddFromAssembly(typeof(MyDatabase).Assembly); + } + + public virtual DbSet Users { get; set; } + } +} \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/Program.cs b/SQLiteEFCodeFirstDemo/Program.cs new file mode 100644 index 0000000..d4ce0b2 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Program.cs @@ -0,0 +1,68 @@ +using System; +using System.Text; +using System.Windows.Forms; + +namespace SQLiteEFCodeFirstDemo +{ + internal static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + private static void Main() + { + //设置应用程序处理异常方式:ThreadException处理 + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + //处理UI线程异常 + Application.ThreadException += Application_ThreadException; + //处理非UI线程异常 + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FormMain()); + } + + private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) + { + string str = GetExceptionMsg(e.Exception, e.ToString()); + MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + //LogManager.WriteLog(str); + } + + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); + MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + //LogManager.WriteLog(str); + } + + /// + /// 生成自定义异常消息 + /// + /// 异常对象 + /// 备用异常消息:当ex为null时有效 + /// 异常字符串文本 + private static string GetExceptionMsg(Exception ex, string backStr) + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine("****************************异常文本****************************"); + sb.AppendLine("【出现时间】:" + DateTime.Now.ToString()); + if (ex != null) + { + sb.AppendLine("【异常类型】:" + ex.GetType().Name); + sb.AppendLine("【异常信息】:" + ex.Message); +#if DEBUG + sb.AppendLine("【堆栈调用】:" + ex.StackTrace); +#endif + } + else + { + sb.AppendLine("【未处理异常】:" + backStr); + } + sb.AppendLine("***************************************************************"); + return sb.ToString(); + } + } +} \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/Properties/AssemblyInfo.cs b/SQLiteEFCodeFirstDemo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3c3f4a5 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("SQLiteEFCodeFirstDemo")] +[assembly: AssemblyDescription("CodeFirst的EF SQLite示例程序")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SQLiteEFCodeFirstDemo")] +[assembly: AssemblyCopyright("©2020 jie65535")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("a8a315f7-36c3-4812-9f09-ef13582e7f68")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/Properties/Resources.Designer.cs b/SQLiteEFCodeFirstDemo/Properties/Resources.Designer.cs new file mode 100644 index 0000000..487ca9b --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace SQLiteEFCodeFirstDemo.Properties +{ + + + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SQLiteEFCodeFirstDemo.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 覆盖当前线程的 CurrentUICulture 属性 + /// 使用此强类型的资源类的资源查找。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/SQLiteEFCodeFirstDemo/Properties/Resources.resx b/SQLiteEFCodeFirstDemo/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/Properties/Settings.Designer.cs b/SQLiteEFCodeFirstDemo/Properties/Settings.Designer.cs new file mode 100644 index 0000000..c7fbd0c --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SQLiteEFCodeFirstDemo.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/SQLiteEFCodeFirstDemo/Properties/Settings.settings b/SQLiteEFCodeFirstDemo/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/SQLiteEFCodeFirstDemo/SQLiteEFCodeFirstDemo.csproj b/SQLiteEFCodeFirstDemo/SQLiteEFCodeFirstDemo.csproj new file mode 100644 index 0000000..b3389f5 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/SQLiteEFCodeFirstDemo.csproj @@ -0,0 +1,118 @@ + + + + + + Debug + AnyCPU + {A8A315F7-36C3-4812-9F09-EF13582E7F68} + WinExe + SQLiteEFCodeFirstDemo + SQLiteEFCodeFirstDemo + v4.5.2 + 512 + true + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll + + + + + + ..\packages\System.Data.SQLite.Core.1.0.113.1\lib\net451\System.Data.SQLite.dll + + + ..\packages\System.Data.SQLite.EF6.1.0.113.0\lib\net451\System.Data.SQLite.EF6.dll + + + ..\packages\System.Data.SQLite.EF6.Migrations.1.0.112\lib\System.Data.SQLite.EF6.Migrations.dll + + + ..\packages\System.Data.SQLite.Linq.1.0.113.0\lib\net451\System.Data.SQLite.Linq.dll + + + + + + + + + + + + + + + + + Form + + + FormMain.cs + + + + + + + FormMain.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + + + + \ No newline at end of file diff --git a/SQLiteEFCodeFirstDemo/packages.config b/SQLiteEFCodeFirstDemo/packages.config new file mode 100644 index 0000000..ba38ab1 --- /dev/null +++ b/SQLiteEFCodeFirstDemo/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file