EF Core динамически игнорирующая колонка - PullRequest
2 голосов
/ 04 февраля 2020

Ниже я пытаюсь динамически игнорировать столбец в базе данных на основе атрибута, установленного в классе сущности. Я не могу понять, что положить на ?????. Я пробовал опору, но ничего хорошего. Пожалуйста, помогите.

            System.Reflection.PropertyInfo[] props = typeof(MyTable).GetProperties();
            foreach (var prop in props)
            {
                object[] attrs = prop.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    DatabaseAttribute dbAttr = attr as DatabaseAttribute;
                    if (dbAttr != null)
                    {
                        string propName = prop.Name;
                        System.Version minVersion = new System.Version(dbAttr.MinVersion);
                        if (databaseVersion < minVersion)
                        {
                            // The database doesn't know about this property, ignore it
                            modelBuilder.Entity<MyTable>().Ignore(d => ?????);
                        }
                    }
                }
            }

1 Ответ

0 голосов
/ 04 февраля 2020
    modelBuilder.Entity<MyTable>().Ignore(prop.Name); 

Или вы можете построить выражение динамически

    modelBuilder.Entity<MyTable>().Ignore(Expression.Property(Expression.Parameter(typeof(MyTable)),prop));
...