简体   繁体   English

我可以将我的Database.SetInitializer调用放入具有基本字符串而不是静态构造函数的上下文构造函数中吗?

[英]Can I put my Database.SetInitializer call in the context constructor with the base string instead of a static constructor?

I am using Entity Framework to store some entities generated at run time using Codedom. 我正在使用实体框架存储使用Codedom在运行时生成的某些实体。 I am actually also creating my contexts dynamically using Codedom too. 实际上,我实际上也在使用Codedom动态创建上下文。 Before I started dynamically generating contexts I was using a hardcoded context. 在开始动态生成上下文之前,我使用的是硬编码上下文。 The start of this hardcoded context class (up to the constructors) looked like this: 这个硬编码上下文类的开始(取决于构造函数)如下所示:

public class EntityModelContext : DbContext, IEntityRegistry
{
    static EntityModelContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EntityModelContext>());
    }

    public EntityModelContext()
        : base("EntityModelContext")
    {
        EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>();
    }

This was all good. 一切都很好。 However, I tried generating this using Codedom and discovered that static isn't a thing with Codedom. 但是,我尝试使用Codedom生成此代码,发现使用Codedom并不是一成不变的事情。 Even if I could generate a static constructor, I was getting an error which basically said that there are two constructors and I don't know which one to use. 即使我可以生成一个静态构造函数,也遇到了一个错误,该错误基本上说有两个构造函数,我不知道要使用哪个构造函数。 So then I merged these two constructors in my Codedom generated context class, and it now looks like this (note it's called TreeContext because I am creating a context for every entity I make and I'm making a tree entity with it in my tests): 因此,然后将这两个构造函数合并到Codedom生成的上下文类中,现在看起来像这样(请注意,它称为TreeContext,因为我正在为所创建的每个实体创建一个上下文,并在测试中使用它来创建树实体) :

TreeContext() : 
        base("TreeContext")
{
    Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TreeContext>());
    EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>();
}

I just tried running a test which generates a tree entity class and an instance of this tree entity, as well as the corresponding tree context class and an instance of this tree context. 我只是尝试运行一个测试,该测试生成一个树实体类和该树实体的实例,以及相应的树上下文类和该树上下文的实例。 I tried inserting this tree entity instance into the tree context instance and it seems to have worked in SQL Server. 我尝试将此树实体实例插入到树上下文实例中,并且它似乎已在SQL Server中工作。 Therefore I guess the constructor works. 因此,我猜构造函数起作用。 However, I wanted to make sure with anyone else who understands better how Database.SetInitializer works. 但是,我想与其他任何人一起确保更好地了解Database.SetInitializer的工作原理。 I thought it had to be called before the base connection string is called, but it seems to work. 我以为必须在调用基本连接字符串之前先调用它,但是它似乎可以工作。 Anyway I have only tested inserting an entity instance. 无论如何,我只测试了插入实体实例。 I haven't tried changing the model or anything more complex. 我没有尝试更改模型或其他更复杂的方法。

You can use SetInitializer where you want. 您可以在需要的地方使用SetInitializer。 Usually is in the static constructor of the context because is static. 通常是在上下文的静态构造函数中,因为它是静态的。

System.Data.Entity.Database.SetInitializer(...);

Usually is called when you first access context functions (is not enough to create the context). 通常在您首次访问上下文函数(不足以创建上下文)时调用。 Usually at first read or at first SaveChanges. 通常在初次阅读或首次保存更改时使用。

You can also generate a static constructor with CodeDom with CodeTypeConstructor instead of CodeContructor. 您还可以使用带有CodeTypeConstructor而不是CodeContructor的CodeDom生成静态构造函数。

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

相关问题 放置Database.SetInitializer的地方 - Place to put Database.SetInitializer Database.SetInitializer中的DropCreateDatabaseIfModelChanges - DropCreateDatabaseIfModelChanges in Database.SetInitializer Database.SetInitializer(new MigrateDatabaseToLatestVersion <Context, Configuration> ()); 错误 - Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>()); Error Database.SetInitializer是否不播种数据库? - Database.SetInitializer is not seeding the database? 使用Database.SetInitializer避免Shotgun手术 - Avoiding Shotgun Surgery with Database.SetInitializer 我可以在构造函数中同时调用this和base重载吗? - Can I call both this and base overloads in a constructor? 为什么我可以调用基本构造函数 - Why I can call base constructor 为什么EF4.1 CodeFirst即使没有“ Database.SetInitializer &lt;&gt;()”调用也会创建数据库? - Why is EF4.1 CodeFirst is creating a database even without a `Database.SetInitializer<>()` call? 如何让重载的构造函数调用默认构造函数以及基本构造函数的重载? - How can I have an overloaded constructor call both the default constructor as well as an overload of the base constructor? Database.SetInitializer的目的 <ArContext> (null)在存储库中? - purpose of Database.SetInitializer<ArContext>(null) inside of repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM