Where should I place fluent mappings?
See original GitHub issueHi there!
I am wandering if I could use fluent mapping
to define all mappings. I am not happy to see models polluted with attributes and tried to use code first
way like EF does and it’s work.
But only place where I think these mappings should be placed is constructor of “context” derived from DataConnection
. But every time I create new “context”, I will get all mapping run again.
Is there any “legal” way to use fluent mappings and not doing ugly things like this to get it run once:
public class Context : DataConnection
{
public Context() : base(LinqToDB.ProviderName.SqlServer2012, "connection string")
{
if (mappingSchema == null)
mappingSchema = InitContextMappings(this.MappingSchema);
}
public ITable<User> Users => GetTable<User>();
public ITable<UserAddress> UserAddress => GetTable<UserAddress>();
private static MappingSchema mappingSchema;
private static MappingSchema InitContextMappings(MappingSchema ms)
{
ms.GetFluentMappingBuilder()
.Entity<User>()
.HasTableName("Users")
.HasPrimaryKey(x => x.UserId).HasIdentity(x => x.UserId);
ms.GetFluentMappingBuilder()
.Entity<UserAddress>()
.HasTableName("UsersAddresses")
.HasPrimaryKey(x => new { x.UserId, x.AddressId })
.Association<User>(x => x.User, (y, z) => y.UserId == z.UserId);
return ms;
}
}
If I would have complex data model, this mapping initialization could consume some noticeable time to execute.
And second question: does L2DB caches it’s context model build with “classic” way with model annotated with attributes, between DataConnection
instances like it happens in EF?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
How to store schema is up to you. If you have issues with static objects, you can put it into your IoC container, if you use any. Also you can just define your mappings using already existing MappingSchema.Default, if you don’t need to support multiple schemas at the same time.
Thank you.