[英]Extension methods for EntityTypeConfiguration and <datatype>PropertyConfiguration
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.