在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; }
    }
View Code

相关文章:

  • 2021-12-29
  • 2021-12-06
  • 2021-07-19
  • 2022-12-23
  • 2021-07-08
  • 2022-12-23
猜你喜欢
  • 2022-03-06
  • 2021-06-20
  • 2021-06-06
  • 2021-12-24
  • 2022-02-09
相关资源
相似解决方案