简体   繁体   English

实体框架5代码优先配置封装

[英]Entity Framework 5 code-first configuration encapsulation

I was wondering (and I hate using the words 'Best Practice') - but is this a good way to approach configuration as it encapsulates the config for AAA? 我想知道(并且我讨厌使用“最佳实践”一词),但这是一种实现配置的好方法,因为它封装了AAA的配置?

I see a lot of examples where the OnModelCreating is a huge list of instructions for creating the database and long methods tell me something isn't quiet right. 我看到很多例子,其中OnModelCreating是创建数据库的大量指令说明,而长方法则告诉我某些事情不是很安静。

public class MyContext : DbContext
{
    public MyContext() : base("name=MyDb") { }

    public DbSet<AAA> AAAs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Configurations.Add(new AAA.Configuration());
    }
}

And the class with a spot for it's config 而带有它的配置的类

public class AAA
{
    [Key] 
    public int Id { get; set; }

    [Required] 
    public string Details { get; set; }

    internal class Configuration : EntityTypeConfiguration<AAA>
    {
        public Configuration()
        {
            // Set all the funky stuff here
        }
    }
}

I get that there probably isn't one right way . 我知道可能没有一种正确的办法 Before I commit a lot of time and tears I am looking for a reason why this might be absolutely the worst idea in the world or if there is a way to do something similar? 在我花很多时间和眼泪之前,我正在寻找一个原因,为什么这可能绝对是世界上最糟糕的想法,或者是否有办法做类似的事情?

EDIT 编辑

A Colleague suggested this as an alternate method using a static property 一位同事建议将此作为使用静态属性的替代方法

public class AAA
{
    [Key] 
    public int Id { get; set; }

    [Required] 
    public string Details { get; set; }

    public static EntityTypeConfiguration<AAA> Configuration
    {
        get { return new AAAConfiguration(); }
    }

}

internal class AAAConfiguration : EntityTypeConfiguration<AAA>
{
   public AAAConfiguration()
   {
            // Set all the funky stuff here
   }
}

I like this as it gives me a bit more flexibility with how the configuration is instanced. 我喜欢它,因为它使我在配置实例方面更具灵活性。

It depends. 这取决于。 I am by now far enough to say that I don't care about db generation. 到目前为止,我已经足够说我不在乎数据库生成了。 It is nice and has advantages (platform independent) which I do not care about. 它很好,并且具有我不在乎的优点(与平台无关)。 it is limiting from using SQL Server fully - so it is back to database projects. 它限制了不能完全使用SQL Server,因此又回到了数据库项目。

This is a world of compromises, and - sorry- a best practice comes with a TON of limitations there. 这是一个折衷的世界,而且-抱歉-最佳实践带有很多限制。

I personally don't see an issue in the approach you have taken. 我个人认为您采用的方法没有问题。 I have just recently resolved a similar dilema in my head, the only difference being is I have the EntityTypeConfiguration<T> classes inside my data layer and not within my model. 我最近刚刚解决了一个类似的难题,唯一的区别是我在数据层内部而不是模型内部拥有EntityTypeConfiguration<T>类。 I'm doing all the mapping logic in these mapping classes too; 我也在这些映射类中完成所有映射逻辑。 this means I don't have to decorate my model with EF specific attributes, keeping them totally persistence-ignorant. 这意味着我不必用EF特定的属性来修饰我的模型,而使它们完全没有持久性。

With your approach, in the OnModelCreating method you only need to have one line per class that you want to persist, plus this code is only hit once if you don't clear the cache that EF creates, so it's only a kind of one-off bootstrap and therefore a long method isn't an issue? 使用您的方法,在OnModelCreating方法中,每个类只需要保留一行即可持久化,而且如果您不清除EF创建的缓存,则只需单击此代码一次,因此,这只是一种-引导程序,因此长方法不是问题吗?

I think your approach is fine, but I'm sure there will be some differing views on the subject. 我认为您的方法很好,但是我敢肯定,在这个问题上会有一些不同的看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM