Excluding columns with Linq2db and NotMapped

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
I have several models with properties without a backing column, tagged with [NotMapped] attributes. These used to work well with Entity Framework but no longer do with Linq2db and FluentMigrator. I tried using Linq2db's own [NotColumn] but it doesn't work either:

The only solution I have found is to modify the FluentMigratorMedataReader.cs core file like this:


protected T GetAttribute<T>(Type type, MemberInfo memberInfo) where T : Attribute
        {
            var attribute = Types.GetOrAdd((type, memberInfo), t =>
            {
                var tableExpr = Expressions.GetOrAdd(type, entityType => _migrationManager.GetCreateTableExpression(entityType));

                if (typeof(T) == typeof(TableAttribute))
                    return new TableAttribute(tableExpr.TableName) { Schema = tableExpr.SchemaName };

                if (typeof(T) != typeof(ColumnAttribute))
                    return null;

                var column = tableExpr.Columns.SingleOrDefault(cd => cd.Name.Equals(NameCompatibilityManager.GetColumnName(type, memberInfo.Name), StringComparison.OrdinalIgnoreCase));

                if (column is null)
                    return null;

                // Modification to exclude properties with the NotMapped or NotColumn attribute

                if (memberInfo.CustomAttributes.Any(a => a.AttributeType == typeof(NotMappedAttribute)))
                    return null;

                if (memberInfo.CustomAttributes.Any(a => a.AttributeType == typeof(NotColumnAttribute)))
                    return null;

                return new ColumnAttribute
                {
                    Name = column.Name,
                    IsPrimaryKey = column.IsPrimaryKey,
                    IsColumn = true,
                    CanBeNull = column.IsNullable ?? false,
                    Length = column.Size ?? 0,
                    Precision = column.Precision ?? 0,
                    IsIdentity = column.IsIdentity,
                    DataType = new SqlDataType((memberInfo as PropertyInfo)?.PropertyType ?? typeof(string)).DataType
                };
            });

            return (T)attribute;
        }


Is there a proper way to achieve this without modifying the core nop files, the model classes, or the database?
3 years ago
See https://www.nopcommerce.com/en/boards/topic/85196/ef-to-linq2db
3 years ago
Thanks, I did read that article and it says the linq2db way to do it is the [NotColumn] attribute but it still doesn't work with FluentMigratorMetadataReader.

I will try with the FluentMigrator Delete.Column() but shouldn't annotations be supported too?
3 years ago
None of the recommendations in the StackOverflow article work out of the box. There doesn't seem a way to exclude columns from models.
2 years ago
Thanks for your contribution, it worked for me.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.