在Code First模式下使用SQLite一直存在不能自动生成数据库的问题,使用SQL Server Compact再转换到SQLite的方式(SQL Server Compact/SQLite Toolbox插件)基本不在我的考虑范围内,直接使用SQL Server Compact性能又是问题。理论上我们可以自己去实现SQLite的Code Frist支持,但实际上我只是在等待它的出现。期待了一年多,SQLite.CodeFirst真的出现了。
1.首先定义实体:
Customer、Role、Category、Post。
public class BaseEntity { public int Id { get; set; } } public class Customer : BaseEntity { public Customer() { this.Roles = new List<Role>(); } public string UserName { get; set; } public virtual ICollection<Role> Roles { get; set; } } public class Role : BaseEntity { public Role() { this.Customers = new List<Customer>(); } public virtual ICollection<Customer> Customers { get; set; } public string RoleName { get; set; } } public class Category : BaseEntity { public Category() { this.Children = new List<Category>(); this.Posts = new List<Post>(); } public int? ParentId { get; set; } public virtual Category Parent { get; set; } public virtual ICollection<Category> Children { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post : BaseEntity { public virtual Category Category { get; set; } }