http://www.cnblogs.com/easygame/p/4447457.html
在Code First模式下使用SQLite一直存在不能自动生成数据库的问题,使用SQL Server Compact再转换到SQLite的方式(SQL Server Compact/SQLite Toolbox插件)基本不在我的考虑范围内,直接使用SQL Server Compact性能又是问题。理论上我们可以自己去实现SQLite的Code Frist支持,但实际上我只是在等待它的出现。期待了一年多,真的出现了。
1.首先定义实体:
Customer、Role、Category、Post。
public class BaseEntity { public int Id { get; set; } } public class Customer : BaseEntity { public Customer() { this.Roles = new List(); } public string UserName { get; set; } public virtual ICollection Roles { get; set; } } public class Role : BaseEntity { public Role() { this.Customers = new List (); } public virtual ICollection Customers { get; set; } public string RoleName { get; set; } } public class Category : BaseEntity { public Category() { this.Children = new List (); this.Posts = new List (); } public int? ParentId { get; set; } public virtual Category Parent { get; set; } public virtual ICollection Children { get; set; } public virtual ICollection Posts { get; set; } } public class Post : BaseEntity { public virtual Category Category { get; set; } }
2.定义实体映射
CustomerMap、RoleMap、CategoryMap和PostMap作为关系表、索引的配置。
public class CustomerMap : EntityTypeConfiguration{ public CustomerMap() { this.Property(o => o.UserName).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = true })); } } public class RolerMap : EntityTypeConfiguration { public RolerMap() { this.HasMany(o => o.Customers).WithMany(o => o.Roles); } } public class CategoryMap : EntityTypeConfiguration { public CategoryMap() { this.HasOptional(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId); } } public class PostMap : EntityTypeConfiguration { public PostMap() { this.HasOptional(o => o.Category).WithMany(o => o.Posts); } }
3.定义初始化数据:
目前SQLite.CodeFist只支持DropCreateDatabaseAlways和CreateDatabaseIfNotExists方式。
public class MyDbInitializer : SqliteDropCreateDatabaseAlways{ public MyDbInitializer(string connectionString, DbModelBuilder modelBuilder) : base(connectionString, modelBuilder) { } protected override void Seed(SqliteDbContext context) { context.Set ().Add(new Customer { UserName = "user" + DateTime.Now.Ticks.ToString(), Roles = new List { new Role { RoleName = "user" } } }); context.Set ().Add(new Post { Category = new Category() }); base.Seed(context); } }
4.定义DbContext:
此处必须配置PluralizingTableNameConvention,否则无法正常使用。
public class SqliteDbContext : DbContext { public SqliteDbContext() : base("DefaultConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); modelBuilder.Configurations.AddFromAssembly(typeof(SqliteDbContext).Assembly);#if DEBUG Database.SetInitializer(new MyDbInitializer(Database.Connection.ConnectionString, modelBuilder));#endif } }
5.配置Web.config:
默认的配置文件各种问题。可以直接拷贝项目中的测试用的配置文件。
6.配置Global.asax:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { using (var db = new SqliteDbContext()) { } AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); } }
查看生成的数据库:表映射、关系映射和索引都正确创建了。
调用一下:
代码已经上传到gitosc: