c# - 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 => ?????);
}
}
}
}
解决方案
modelBuilder.Entity<MyTable>().Ignore(prop.Name);
或者您可以动态构建表达式
modelBuilder.Entity<MyTable>().Ignore(Expression.Property(Expression.Parameter(typeof(MyTable)),prop));
推荐阅读
- c# - 发布时迁移到 .NET 6.0 错误:资产文件没有“net5.0”的目标
- r - 只提取数字
- php - 使用 .htaccess 阻止对目录的 GET 请求
- selenium-webdriver - 未生成范围报告:获取异常 java.lang.noclassdeffounderror:freemarker/template/templateexceptionhandler
- python - 如何可视化qgis中点之间的路线距离?
- javascript - 如何将此 toast 组件导入我的按钮?
- javascript - 在网页上捕获音频流
- javascript - Angular:如何从另一个可观察对象中的可观察对象获取价值
- python - 为什么redis流消费者应该在同一个消费者组中指定一个单独的名字?
- c# - 将除 2 和 5 以外的所有 char 数组转换为 int