Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取实体框架中被忽略的属性_C#_Entity Framework_Ef Code First_Entity Framework 6_Entity Framework 5 - Fatal编程技术网

C# 获取实体框架中被忽略的属性

C# 获取实体框架中被忽略的属性,c#,entity-framework,ef-code-first,entity-framework-6,entity-framework-5,C#,Entity Framework,Ef Code First,Entity Framework 6,Entity Framework 5,我与EF合作开发一个框架。我想获取一个实体的所有被忽略的属性来构建一些特殊的查询。我怎么做 public class Customer { public int Id { get; set; } public DateTime BirthDate { get; set; } public int Age { get; set; } } public class CustomerContext : DbContext { protected override vo

我与EF合作开发一个框架。我想获取一个实体的所有被忽略的属性来构建一些特殊的查询。我怎么做

public class Customer
{
    public int Id { get; set; }
    public DateTime BirthDate { get; set; }
    public int Age { get; set; }
}

public class CustomerContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Customer> Customers { get; set; }
}

public static class DbContextExtensions
{
    public static List<string> GetIgnoredProperties(this DbContext context, string entityTypeName)
    {
         // ???
    }
}
公共类客户
{
公共int Id{get;set;}
公共日期时间出生日期{get;set;}
公共整数{get;set;}
}
公共类CustomerContext:DbContext
{
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Ignore(customer=>customer.Age);
基于模型创建(modelBuilder);
}
公共数据库集客户{get;set;}
}
公共静态类DbContextensions
{
公共静态列表GetIgnoredProperties(此DbContext上下文,字符串entityTypeName)
{
// ???
}
}

我知道这并没有回答您最初的问题,我在评论中提到您应该使用反射,但那只是因为我看错了您的问题

这里有一个使用反射的替代方法,因为如果你做得不对的话

如果将
[NotMapped]
属性指定给类中要忽略的属性,则可以使用反射检索所有
[NotMapped]
属性。下面是如何实现这一目标的示例

var resultArray = yourClassInstance.GetType().GetProperties()
                            .Where(prop => Attribute.IsDefined(prop, typeof(NotMappedAttribute)));

希望这对您有所帮助。

您可以通过调用。它将根据DbModelBuilder的配置设置创建DbModelDbModel公开一个包含上下文使用的类型的概念模型。该函数保存上下文中声明的每个类型,对于每个类型,它保存在配置期间未被DbModelBuilder忽略的属性。因此,为了实现您想要的,您必须将每个实体类型的属性与EdmModel中存在的属性相交。它将给出它们之间的增量,即忽略属性之前的增量。这里有一个例子:

public class CustomerContext : DbContext
{
    private static IReadOnlyDictionary<Type, IReadOnlyCollection<PropertyInfo>> _ignoredProperties;
    /// Hold the ignored properties configured from fluent mapping
    public static IReadOnlyDictionary<Type, IReadOnlyCollection<PropertyInfo>> IgnoredProperties
    {
        get
        {
            return _ignoredProperties;
        }
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);

        // Build ignored properties only if they are not                
        if (_ignoredProperties == null)
        {                
            var model = modelBuilder.Build(this.Database.Connection);                
            var mappedEntityTypes = new Dictionary<Type, IReadOnlyCollection<PropertyInfo>>();
            foreach (var entityType in model.ConceptualModel.EntityTypes)
            {
                var type = Type.GetType(entityType.FullName);
                var typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                var mappedProperties = entityType.DeclaredProperties.Select(t => t.Name)
                    .Union(entityType.NavigationProperties.Select(t => t.Name));
                mappedEntityTypes.Add(type, new ReadOnlyCollection<PropertyInfo>(
                    typeProperties.Where(t => !mappedProperties.Contains(t.Name)).ToList()));
            }
            _ignoredProperties = new ReadOnlyDictionary<Type, IReadOnlyCollection<PropertyInfo>>(mappedEntityTypes);
        }

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Customer> Customers { get; set; }
}
欺骗: 使用这种方法,DbModel将被构建两次,一次用于收集被忽略的属性,第二次由EntityFramework在缓存对象时进行,以便将来创建ObjectContext。它可能会影响DbContext的冷启动,这意味着第一次在上下文上执行查询时,它会稍微慢一点。它将取决于DbContext的大小。热情的询问不应该受到影响

赞成的意见:
对deDbModelBuilder配置所做的所有更改将自动反映在IgnoredProperties属性中。

Hmm,我看错了您的问题。但是,无论如何,我会发布一个替代方案,使用反射和[NotMapped]属性。您使用的是哪个版本的EntityFramework?我想知道您采取了什么方法来实现这一点?Fluent API中的[NotMapped]等效于像您一样使用忽略,但是如果您已经定义了属性的类,我认为将[NotMapped]属性放在您希望忽略的属性上没有任何害处。流畅的API方式和你的类上的正常属性配合得很好。我想听听你诚实的意见。你认为利大于弊吗+感谢您的努力。这将取决于应用程序的工作方式。如果没有太多的停机时间,那么成本是值得的,因为只有预热受到影响。另一方面,如果应用程序像桌面应用程序一样经常启动和停止,它将不会启动。我将更新我的答案以提供一些基准。顺便说一句,谢谢你的支持。我暂时删除了基准测试,因为它们是错误的,没有调用onmodelcreating。
var properties = CustomerContext.IgnoredProperties[typeof(Customer)];