Article Directory
foreword
I posted a video on station B before about how to use the EF framework to generate entity classes. I did the adaptation of Mysql, Sql server, and Sqlite at that time. But the comment section below says that SqlSuger is very useful, and many companies use SqlSuger.
B station video: How to quickly develop database business in C#, sql server sqlite mysql
SqlSuger
SqlSuger seems to be a domestic ORM framework, similar to Spirngboot's MyBatis.
SqlSuger official website
Test DB Fisrt and CodeFirst
Create a new .NET Core console program
The following project path
- Sqlsuger
- DB: database entity class
- Student entity class
- Utils: SqlSuger operation object
- SqlSugerHelper
- Program.cs
- DB: database entity class
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 entity class
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;}
}
Just execute it directly.
- SqlSugerHelper.CodeFirst()。
- A new database needs to be created. If there is no table with the same name, create a table. If there is a table with the same name, skip it.
- SqlSugerHelper.DB_First()
- The created file is under the DB of the Debug folder. The namespace defaults to Models. If you need to change, you need to modify the configuration generated by the file. There is a configuration tutorial on the SqlSuger official website, but I personally recommend using the default namespace, that is, Models to store entity classes.