Fluent mappings including conventional - schotime/NPoco GitHub Wiki
To create a mapping for a class you can inherit from Map<T> where T is the class you are mapping for.
public class UserMapping : Map<User>
{
public UserMapping()
{
PrimaryKey(x => x.UserId);
TableName("Users");
Columns(x =>
{
x.Column(y => y.Name).Ignore();
x.Column(y => y.Age).WithName("a_ge");
});
}
}Mappings can also inherit from Mappings and specify all mappings in one class using the For<> method.
public class OurMappings : Mappings
{
public OurMappings()
{
For<User>().Columns( ....
}
}You only want to create the mappings once and we do this using a Database Factory.
public void Application_Start()
{
MyFactory.Setup();
}public static class MyFactory
{
public static DatabaseFactory DbFactory { get; set; }
public static void Setup()
{
var fluentConfig = FluentMappingConfiguration.Configure(new OurMappings());
//or individual mappings
//var fluentConfig = FluentMappingConfiguration.Configure(new UserMapping(), ....);
DbFactory = DatabaseFactory.Config(x =>
{
x.UsingDatabase(() => new Database("connString"));
x.WithFluentConfig(fluentConfig);
x.WithMapper(new Mapper());
});
}
}Then you can use it like so in your code.
var database = MyFactory.DbFactory.GetDatabase();If you are using a container then you could have something like
For<IDatabase>().Use(() => MyFactory.DbFactory.GetDatabase());