EntityTypeConfiguration和< datatype> PropertyConfiguration的扩展方法 [英] Extension methods for EntityTypeConfiguration and <datatype>PropertyConfiguration
本文介绍了EntityTypeConfiguration和< datatype> PropertyConfiguration的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Entity Framework,我可能具有如下配置:
With Entity Framework I might have a configuration that looks like:
internal class MyDbContext : DbContext
{
....
protected override void OnModelCreating(DbModelBuilder mb)
{
builder.Entity<MyEntity>()
.ToTable("MyTable", "MySchema");
builder.Entity<MyEntity>()
.Property(e => e.Name)
.IsRequired()
.HaxMaxLength(10);
builder.Entity<MyEntity>()
.Property(e => e.City)
.HaxMaxLength(10);
}
}
我想写一个扩展方法,因此我可以这样写:
I'd like to write an extension method so I could write it like:
builder.Entity<MyEntity>()
.ToTable("MyTable", "MySchema")
.Property(e => e.Name,
n => n.IsRequired()
.HaxMaxLength(10))
.Property(e => e.City,
c => c.HasxMaxLength(50));
我很确定我的签名正确,但是我不知道如何获得内部管道正常工作。
I'm pretty sure I have the signature correct, but I don't know how to get the inner plumbing to work correctly.
public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, byte[]>> propertyExpression,
Func<BinaryPropertyConfiguration, BinaryPropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
Func<TEntityType, byte[]> func = propertyExpression.Compile();
// ??
return instance;
}
推荐答案
我意识到我不需要执行/编译任何程序,只需要按正确的顺序将参数链接在一起即可。
After a little playing around, I realized I don't need to execute/compile any of it, I just need to chain the parameters together in the correct sequence.
public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, byte[]>> propertyExpression,
Func<BinaryPropertyConfiguration, BinaryPropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));
return instance;
}
这篇关于EntityTypeConfiguration和< datatype> PropertyConfiguration的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文