12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using SQLite.CodeFirst;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Data.Entity;
- using System.Data.Entity.Migrations;
- using System.Data.Entity.ModelConfiguration.Conventions;
- using System.Data.SQLite.EF6.Migrations;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SqliteEFDemo
- {
- internal sealed class ContextMigrationConfiguration : DbMigrationsConfiguration<SQLiteContext>
- {
- public ContextMigrationConfiguration()
- {
- // 启用自动迁移
- AutomaticMigrationsEnabled = true;
- // 允许自动迁移导致的数据丢失
- AutomaticMigrationDataLossAllowed = true;
- SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());// the Golden Key
- ContextKey = "SQLiteDemoContext";
- }
- protected override void Seed(SQLiteContext context)
- {
- // This method will be called after migrating to the latest version.
- // You can use the DbSet<T>.AddOrUpdate() helper extension method
- // to avoid creating duplicate seed data.
- }
- }
- public class SQLiteContext:DbContext
- {
- static SQLiteContext()
- {
- Database.SetInitializer(new MigrateDatabaseToLatestVersion<SQLiteContext, ContextMigrationConfiguration>(true));
- }
- public SQLiteContext()
- : base("SqliteConn")
- {
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- //解决EF动态建库数据库表名变为复数问题
- // modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
- //如果不存在数据库,则创建
- //Database.SetInitializer(new SqliteCreateDatabaseIfNotExists<SQLiteContext>(modelBuilder));
- }
- // 数据库表文件
- public DbSet<Person> Persons { get; set; }
- }
- [Table("Person")]
- public class Person
- {
- [Key]
- public Int64 Id { get; set; } //注意要用Int64
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string enable { get; set; }
- public string enable2 { get; set; }
- public string enable3 { get; set; }
- public string enable4 { get; set; }
- }
- }
|