忽略Entity Framework 6中的所有属性,但忽略一些属性

时间:2015-06-26 07:01:00

标签: c# entity-framework entity-framework-6

我想使用Entity Framework在数据库上保留一些数据 我有一些更大的POCO但我只想存储一些属性。

我知道我可以使用Fluent API方法通过Ignore()实现此目的。但是,是否有可能不仅忽略已定义的属性,而且忽略定义的所有属性? 所以如果你有这样的POCO:

public class MyPoco
{
    public int Id { get; set; }
    public string Name { get; set; }
    .
    .
    .
    public int SomeSpecialId { get; set; }
}

你只想存储IdSomeSpecialId,你会这样做:

protected override void OnModelCreating(DbModelBuilder builder)
{
    builder.Entity<MyPoco>().Ignore(x => x.Name);
    builder.Entity<MyPoco>().Ignore(x => x.WhatEver);
    .
    .
    .
    // ignore everything but Id and SomeSpecialId
    base.OnModelCreating(builder);
}

现在的问题是,如果您必须扩展POCO但不想保留这些扩展属性,则还必须更改OnModelCreating()方法。那么有没有办法做这样的事情:

public override void OnModelCreating(DbModelBuilder builder)
{
    builder.Entity<MyPoco>().IgnoreAllBut(x => x.Id, x.SomeSpecialId);
    base.OnModelCreating(builder);
}

2 个答案:

答案 0 :(得分:3)

您可以编写一个可以执行此操作的扩展方法。代码并不简单,因为您需要使用表达式树。

这是您的IgnoreAllBut方法:

public static EntityTypeConfiguration<T> IgnoreAllBut<T>(this EntityTypeConfiguration<T> entityTypeConfiguration,
        params Expression<Func<T, object>>[] properties) where T : class
{
    // Extract the names from the expressions
    var namesToKeep = properties.Select(a =>
    {
        var member = a.Body as MemberExpression;
        // If the property is a value type, there will be an extra "Convert()"
        // This will get rid of it.
        if (member == null)
        {
            var convert = a.Body as UnaryExpression;
            if (convert == null) throw new ArgumentException("Invalid expression");
            member = convert.Operand as MemberExpression;
        }
        if (member == null) throw new ArgumentException("Invalid expression");
        return (member.Member as PropertyInfo).Name;
    });
    // Now we loop over all properties, excluding the ones we want to keep
    foreach (var property in typeof(T).GetProperties().Where(p => !namesToKeep.Contains(p.Name)))
    {
        // Here is the tricky part: we need to build an expression tree
        // to pass to Ignore()
        // first, the parameter
        var param = Expression.Parameter(typeof (T), "e");
        // then the property access
        Expression expression = Expression.Property(param, property);
        // If the property is a value type, we need an explicit Convert() operation
        if (property.PropertyType.IsValueType)
        {
            expression = Expression.Convert(expression, typeof (object));
        }
        // last step, assembling everything inside a lambda that
        // can be passed to Ignore()
        var result = Expression.Lambda<Func<T, object>>(expression, param);
        entityTypeConfiguration.Ignore(result);
    }
    return entityTypeConfiguration;
}

答案 1 :(得分:1)

您可以在类本身中将单个属性标记为NotMapped。

public class MyPoco
{
    public int Id { get; set; }

    [NotMapped]
    public string Name { get; set; }

    public int SomeSpecialId { get; set; }
}

没有解决你忽略一切的问题,除了这个&#39;但是可能会明确包括什么内容。