文章目录
- 前言
 - SqlSuger
 - 测试DB Fisrt和CodeFirst
 
前言
我之前在B站上面发视频说如何使用EF框架去生成实体类。我当时做了Mysql,Sql server,Sqlite的适配。但是下面评论区说SqlSuger很好用,而且很多公司都用SqlSuger。
B站视频:C#如何快速开发数据库业务,sql server sqlite mysql

 
SqlSuger
SqlSuger好像是一个国产的ORM框架,类似于Spirngboot的MyBatis。
 SqlSuger官网
测试DB Fisrt和CodeFirst
新建一个.NET Core控制台程序

 如下项目路径
 
- Sqlsuger 
  
- DB:数据库实体类 
    
- Student实体类
 
 - Utils:SqlSuger操作对象 
    
- SqlSugerHelper
 
 - Program.cs
 
 - DB:数据库实体类 
    
 
SqlSugerHelper
namespace SqlSuger.Utils
{
    public class SqlSugerHelper
    {
        public SqlSugarScope M_SqlSugarScope { get; set; } = new SqlSugarScope(new ConnectionConfig()
        {
            DbType = DbType.SqlServer,
            ConnectionString = "server=.;database=SqlSugarTest;uid=username;pwd=password",
            IsAutoCloseConnection = true,
        });
        public void CodeFirst()
        {
            M_SqlSugarScope.CodeFirst.InitTables(typeof(Student));
           
        }
        public void DB_First()
        {
        	//
            M_SqlSugarScope.DbFirst.IsCreateAttribute().CreateClassFile(@"DB");
        }
    }
}
 
Student实体类
	public partial class Student
    {
           [SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
           public int Id {get;set;}
       
           public string Name {get;set;}
       
           public int Age {get;set;}
        
           public DateTime CreateTime {get;set;}
    }
 
直接执行就可以了。
- SqlSugerHelper.CodeFirst()。 
  
- 需要新建数据库,如果没有同名表则建表,如果有同名表则跳过。
 
 - SqlSugerHelper.DB_First() 
  
- 创建的文件在Debug文件夹的DB下面。命名空间默认为Models。如果需要改变则需要修改文件生成的配置,SqlSuger官网上面有配置教程,不过个人建议使用默认命名空间,即Models存放实体类。
 
 



















