使用lambda表达式将反射属性传递给.HasKey()

问题描述 投票:0回答:1

我希望有一个灵活的Entity DbContext,我可以将我的数据库优先模式放在一个嵌套类中。如果每个表都有一个名为Id的列作为主键,则下面的poc实际上有效。但是如果主键不同,我需要指定主键。

我可以使用Entity的[Key]属性指定密钥,它实际上可以工作。但我不想这样做,因为我想将实体代码与模式定义分开。我宁愿编写一个自定义属性,并编写一些逻辑来检查自定义属性的列。

通常我会使用entityTypeConfiguration.HasKey(MyTable => myTable.MyKey);将所需属性设置为主键。但我不知道如何编写正确的Lambda表达式来设置所需的属性,因为我正在使用泛型和反射。

这是我的代码:

using MySql.Data.Entity;
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;

class MySchema // my database
{
    public class MyTable // a table inside mySchema
    {
        public int MyKey { get; set; } // a column inside myTable
        public string MyValue { get; set; } // another column inside myTable
    }
}

static class Program
{
    static void Main()
    {
        var myTable = new EfDataContext<MySchema>().Set<MySchema.MyTable>();
        Console.Write(myTable.Where(a => a.MyKey == 1).First().MyValue);
    }
}

[DbConfigurationType(typeof(MySqlEFConfiguration))] // using mysql
public class EfDataContext<Schema> : DbContext
{
    public EfDataContext() : base("db") { } // use connection string "db"
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); // let's assume my naming is like in the database

        foreach (Type table in typeof(Schema).GetNestedTypes())
        {
            // type table is now a variable, so we cannot use it as a generic like: this.createEntity<table>(modelBuilder);
            this.GetType().GetMethod("CreateEntity").MakeGenericMethod(table).Invoke(this, new object[] { modelBuilder });
        }
    }

    public void CreateEntity<Table>(DbModelBuilder modelBuilder) where Table : class
    {
        var entityTypeConfiguration = modelBuilder.Entity<Table>();

        // So far, so good!
        // But if there is no column named "Id", I need to tell EF which column is the primary key.
        // If I weren't using a generic type, I would that column MyKey is the primary key of table MyTable like this:
        // entityTypeConfiguration.HasKey(row => row.MyKey);
        // But since now I am using a generic type as the table, and propertyInfo appears to be the primary key, what do I say?

        foreach (var propertyInfo in typeof(Table).GetProperties())
        {
            if (propertyInfo.DeclaringType.Name == "MyTable" && propertyInfo.Name == "MyKey") // just a silly example for now
            {
                // this throws an InvalidOperationException: The properties expression 'row => value(EfDataContext`1+<>c__DisplayClass2_0`1[MySchema,MySchema+MyTable]).propertyInfo' is not valid. The expression should represent a property: C#: 't => t.MyProperty'  
                entityTypeConfiguration.HasKey(row => propertyInfo);
            }
        }
    }
}
c# lambda entity-framework-6 ef-fluent-api
1个回答
1
投票

您可以创建以下扩展方法

public static class ConfigurationExtensions
{
    public static EntityTypeConfiguration<T> HasKey<T>(this EntityTypeConfiguration<T> config, PropertyInfo property) where T : class
    {
        //entity type
        ParameterExpression parameter = Expression.Parameter(typeof(T));
        //entity.key
        Expression prop = Expression.Property(parameter, property.Name);

         //entity => entity.key
        var lambda = Expression.Lambda(prop, parameter);

        MethodInfo hasKeyMethod = typeof(EntityTypeConfiguration<T>)
            .GetMethods(BindingFlags.Public | BindingFlags.Instance)
            .Where(m => m.Name == nameof(EntityTypeConfiguration<T>.HasKey)
                && m.GetParameters().Count() == 1)
            .First()
            .MakeGenericMethod(property.PropertyType);

        return (EntityTypeConfiguration<T>)hasKeyMethod.Invoke(config, new object[] { lambda });
    }
}

并使用它

entityTypeConfiguration.HasKey(propertyInfo);
© www.soinside.com 2019 - 2024. All rights reserved.