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

152 Views Asked by At

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'

2

There are 2 best solutions below

0
Svyatoslav Danyliv On BEST ANSWER

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();
    }
}
0
Mehdi On

At first initialize FluentMappingBuilder with current class

var builder = new FluentMappingBuilder(this);

the Don't forget to call builder.Build() at the end.