Search code examples
c#asp.net.netlinq2db

.Net linq2db, fluentMappingBuilder, table name not apply for query


I'm diving into a little project with linq2db. I've set up models, a connection, and a simple query. But here's the thing: every time I run the query, linq2db uses the table name from the entity model, not the name from fluentmapper. Am I missing something about using MappingSchema() and FlumentMappingBuilder? Help a newbie in linq2db out? 🤔 Im using sqlite db...

public class SparkleContext : DataConnection
    {
        public SparkleContext(DataOptions<SparkleContext> options)
            : base(options.Options)
        {
            AddMappingSchema(new SparkleMappingSchema());
        }

        public ITable<CompanyEm> Companies => this.GetTable<CompanyEm>();

        public ITable<MeterEm> Meters => this.GetTable<MeterEm>();

        public ITable<ReadingsEm> Readings => this.GetTable<ReadingsEm>();
    }

and the mapping schema

   public class SparkleMappingSchema : MappingSchema
    {
        public SparkleMappingSchema()
        {
            var mappings = new MappingSchema();

            var builder = new FluentMappingBuilder(mappings);

            builder.Entity<CompanyEm>().HasTableName("Companies")
                .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
                .Property(x => x.Name).HasColumnName("Name").IsNotNull()
                .Property(x => x.Description).HasColumnName("Description").IsNullable()
                .Property(x => x.Meters).HasColumnName("Meters").IsNullable()
                .Property(x => x.CreatedAt).HasColumnName("CreatedAt").IsNullable()
                .Property(x => x.UpdatedAt).HasColumnName("UpdatedAt").IsNullable();

            builder.Entity<MeterEm>().HasTableName("Meters")
                .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
                .Property(x => x.Name).HasColumnName("Name").IsNotNull()
                .Property(x => x.Readings).HasColumnName("Readings").IsNullable()
                .Property(x => x.CreatedAt).HasColumnName("CreatedAt").IsNullable()
                .Property(x => x.UpdatedAt).HasColumnName("UpdatedAt").IsNullable();

            builder.Entity<ReadingsEm>().HasTableName("Readings")
                .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
                .Property(x => x.Time).HasColumnName("Time").IsNotNull()
                .Property(x => x.Value).HasColumnName("Value").IsNotNull()
                .Property(x => x.CreatedAt).HasColumnName("CreatedAt").IsNullable()
                .Property(x => x.UpdatedAt).HasColumnName("UpdatedAt").IsNullable();
        }
    }

and small query

 using var sparkleDb = new DataConnection(context.DataProvider, context.ConnectionString);

            var test = context.Companies.Where(x => x.Name == "God_Company").Select(x => x.Id).AsQueryable();

enter image description here

and error is something like this System.Data.SQLite.SQLiteException: 'SQL logic error no such table: CompanyEm'


Solution

  • It is because you have defined mapping for wrong MappingSchema. Create FluentMappingBuilder with this parameter.

    public class SparkleMappingSchema: MappingSchema
    {
        public SparkleMappingSchema()
        {
            var builder = new FluentMappingBuilder(this);
    
            builder.Entity<CompanyEm>().HasTableName("Companies")
                .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
                .Property(x => x.Name).HasColumnName("Name").IsNotNull()
                .Property(x => x.Description).HasColumnName("Description").IsNullable()
                .Property(x => x.Meters).HasColumnName("Meters").IsNullable()
                .Property(x => x.CreatedAt).HasColumnName("CreatedAt").IsNullable()
                .Property(x => x.UpdatedAt).HasColumnName("UpdatedAt").IsNullable();
    
            .....
    
            builder.Build();
        }
    }