DapperExtensions and Dapper.Contrib在表构架不是默认dbo时的处理 DapperExtensions and Dapper.Contrib with non-dbo Schema

时间:2023-03-09 04:32:29
DapperExtensions and Dapper.Contrib在表构架不是默认dbo时的处理  DapperExtensions and Dapper.Contrib with non-dbo Schema

什么是数据库的Schema

dbo是一个构架(schema),与sql2000不同的是,在sql2005中,表的调用格式如下:"数据库名.构架名.表名",同一个用户可以被授权访问多个构架,也可以被禁止访问某个或多个构架,这就是2005中提倡的"用户与构架分离"的概念.在2005中,如果在创建表时没有指定构架(schema),那么系统默认该表的构架是dbo,所以会出现很多表名前自动加上dbo.字符样式.

DapperExtensions 下的方法

Using the built in ClassMapper, you can customize the mapping for a specific entity.

public class MyModelMapper : ClassMapper<MyModel>
{
public MyModelMapper()
{ //use different table name
Table("table_name"); //use a custom schema
Schema("not_dbo_schema"); //have a custom primary key
Map(x => x.ThePrimaryKey).Key(KeyType.Assigned); //Use a different name property from database column
Map(x=> x.Foo).Column("Bar"); //Ignore this property entirely
Map(x=> x.SecretDataMan).Ignore(); //optional, map all other columns
AutoMap();
}
}

Dapper.Contrib下的方法

You can use Table attribute to show schema and table name together with a dot in between:

using Dapper.Contrib.Extensions;

[Table ("vehicles.YourTables")]
public class YourClass
{
public int Id { get; set; }
public string Name { get; set; }
}