Linq2Db attributes are not working in Domain

8 месяцев назад
I have a domain with a not mapped column. To resolve invalid column name error I used NotColumn attribute in my domain but at runtime, I'm still getting the invalid column error. I think linq2db attributes are ignored by fluent migratory at run time. is there any way I could achieve the same using fluent migratory. PFB my domain and builder class

using Nop.Core;
using LinqToDB.Mapping;

namespace Nop.MyProject.Core.Domain
{
    public class MyDomain: BaseEntity
    {
        public string Name { get; set; }
        [NotColumn]
        public string ForattedName { get; set; }
    }
}

using FluentMigrator.Builders.Create.Table;
using Nop.Data.Mapping.Builders;
using Nop.MyProject.Core.Domain;

namespace Nop.MyProject.Core.Data
{
    public class RoleViewBuilder : NopEntityBuilder<MyDomain>
    {
        public override void MapEntity(CreateTableExpressionBuilder table)
        {
            table.InSchema("myp"));
        }
    }
}
3 месяца назад
The only way I've found to make this work is by modifying FluentMigratorMetadataReader 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;

                // * * * * Modification to exclude NotColumn attributes * * * *
                var column = tableExpr.Columns.SingleOrDefault(cd => cd.Name.Equals(NameCompatibilityManager.GetColumnName(type, memberInfo.Name), StringComparison.OrdinalIgnoreCase));

                if (column is null)
                    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;
        }
один месяц назад
antoniodlp wrote:
The only way I've found to make this work is by modifying FluentMigratorMetadataReader 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;

                // * * * * Modification to exclude NotColumn attributes * * * *
                var column = tableExpr.Columns.SingleOrDefault(cd => cd.Name.Equals(NameCompatibilityManager.GetColumnName(type, memberInfo.Name), StringComparison.OrdinalIgnoreCase));

                if (column is null)
                    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;
        }


Yes its working for me thanks!