question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Where should I place fluent mappings?

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, May 23, 2018

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.

0reactions
Usagacommented, May 24, 2018

Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fluent mapping | Fluent NHibernate Wiki - Fandom
Fluent NHibernate's interface is designed for discoverability. Everything you need should be easy to find "under" the declaring method using method chains.
Read more >
Fluent API - Configuring and Mapping Properties and Types
All properties of Department will be mapped to columns in a table called t_ Department.
Read more >
Configure Property Mappings using Fluent API in EF Code- ...
The Fluent API can be used to configure properties of an entity to map it with a db column. Using Fluent API, you...
Read more >
How to add mappings by namespace in Fluent NHibernate
I am handling this in NHibernate by creating one SessionFactory per database (I assume this is the correct thing to do). So I...
Read more >
Implement the fluent mappings - Sitefinity CMS Modules
To use the mappings in the data provider, you must return an instance of the ProdusFluentMetadataSource in the GetMetaDataSource method of the provider....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found