C# SqlSugar手动生成实体类后根据实体类生成数据库
时间: 2023-08-02 09:42:32 浏览: 152
在 SqlSugar 中,可以通过实体类生成数据库表,具体步骤如下:
在实体类中添加
[SugarTable("表名")]
标记,指定表名。例如:[SugarTable("Student")] public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }
在 SqlSugar 配置中启用实体类的属性,例如:
public static void Main(string[] args) { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "Data Source=.;Initial Catalog=TestDb;Integrated Security=True", DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute // 启用属性 }); // 根据实体类生成数据库表 db.CodeFirst.InitTables(typeof(Student)); }
通过以上步骤,就可以根据实体类生成对应的数据库表。需要注意的是,生成数据库表前需要先建立好对应的数据库,否则会报错。
相关推荐















