首页 > 解决方案 > EF Core 动态忽略列

问题描述

下面我试图根据实体类中设置的属性动态忽略数据库中的列。我不知道该放什么??????。我试过道具但没有好处。请帮忙。

            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 => ?????);
                        }
                    }
                }
            }

标签: c#ef-core-2.0

解决方案


    modelBuilder.Entity<MyTable>().Ignore(prop.Name); 

或者您可以动态构建表达式

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

推荐阅读