我希望以一种灵活的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);
}
}
}
}
答案 0 :(得分:1)
您可以创建以下扩展方法
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
@Autowired
private WebApplicationContext context;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
并使用它
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 });
}
}