How does entityframework Core 1.1 create an DbContext instance?

Source: Internet
Author: User

Explicitly creating an DbContext instance through a constructor with onconfiguring

This must be our simplest way, by calling a class that inherits from DbContext and calling its parameterless constructor, and we need to remember that whenever we instantiate it we need to release it, which is to wrap its instance in a using. As follows:

using (var context = new Efcorecontext ()) {}

The EF core Context instance is then configured by overloading Onconfiguring, as follows.

public class efcorecontext:dbcontext{protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) = = Optionsbuilder.usesqlserver (@ "server=.;D Atabase=eftest; Trusted_connection=true; ");}

Note: Overloaded onconfiguring is not the same as the onmodelcreating creation model in the previous EF version, Onmodelcreating creates a model context that is instantiated only once, But Onconfiguring is called once every time a context is instantiated, so onconfiguring can take full advantage of the constructor or other data in the context.

In EF 6.x there are many constructors for contexts, such as connection string parameters, which are also available in EF Core 1.1:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

public class efcorecontext:dbcontext{private readonly string _connectionstring;    Public Efcorecontext (String connectionString) {_connectionstring = connectionString; } protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) = Optionsbuilder.usesqlserv ER (_connectionstring);}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Using Dbcontextoptions with no dependency injection

In DbContext's constructor we can accept a Dbcontextoptions object, which is used primarily when creating an DbContext instance in a Di container, and of course it can be explicitly called, By creating a Dbcontextoptions object to be isolated from the context, it can use the same options for each instance of the context, as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

public class Efcorecontext:dbcontext {public efcorecontext (dbcontextoptions options): Base (option s) {}}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

    public class homecontroller : controller    {         private static dbcontextoptions _contextoptions ;         public iactionresult index ()          {            _ Contextoptions = new dbcontextoptionsbuilder ()              . Usesqlserver ("")             . options;            using  (Var context  = new efcorecontext (_contextoptions))              {            }             return view ();        }     }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Seeing here we see that there is no need to reload onconfiguring, but onconfiguring will still be overloaded and called, why so Because we inject the context into the configuration, it calls the constructor and simultaneously makes the appropriate adjustments to the onconfiguring.

Creating an DbContext instance using dependency Injection

We just register the context type in the Di container, and then it will be parsed by the DI container, but the context is registered in the DI container and we don't have to worry about it, you have to notice the following two points. We typically inject it into the DI container to do so.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

public class Efcorecontext:dbcontext {public efcorecontext (dbcontextoptions options): Base (option s) {}}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Services. adddbcontext<efcorecontext> (options = options.            Usesqlserver (sqlstr, d = d.migrationsassembly ("Studyefcore")); });

reasonable and lawful, but why not use the following

Services. Addsingleton<efcorecontext> ();

Is it not possible to inject in a single case, if you do, you wait for the program to crash, because the context dbcontext is not thread-safe, that is, it cannot be registered to a singleton to use no extra locks. And then after injecting it into the Di container, when we use it or use it to wrap it, you don't think it's going to be injected into the DI container. It does everything for you, the DI container does not automatically handle for you, of course, if you are only temporarily used in the Di container, it is usually not a disaster. So we need to remember the following two points.

(1) When the context is injected into the DI container, the use of DbContext is still included, as the Di container does not automatically release it.

(2) DbContext is non-thread-safe, even in the Di container, there is nothing to ensure that there is no problem, most of the situation is not problematic, do not worry.

Di with dbcontextoptions create DbContext instance

Whether we register a dbcontextoptions instance in Di is still a bit of a use, it will only be created once and used as a singleton. For example, we can initialize the data, as follows

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

public class Efcorecontext:dbcontext {public efcorecontext (dbcontextoptions options): Base (option s) {}}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

        private static iserviceprovider _ Serviceprovider;        public iactionresult index ()          {             var contextoptions = new dbcontextoptionsbuilder ()             . Usesqlserver ("")            . options;            var services =  New servicecollection ()                  . Addsingleton (contextoptions)                  . Addscoped<efcorecontext> ();            &nBsp;_serviceprovider = services. Buildserviceprovider ();            return  View ();         }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Now the Efcorecontext context will be parsed by the DI container, and the Dbcontextoptions instance will be injected into its constructor. This _serviceprovider is the provider of the injection context, and we have said that we can initialize the data, how to initialize the Nikki, let's see. The following configuration methods are used in Startup.cs:

public void Configure (Iapplicationbuilder app, Ihostingenvironment env, Iloggerfactory loggerfactory) {..        .. }

The app has the following properties:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/589642/201612/589642-20161226233444507-2091193673. PNG "style=" margin:0px;padding:0px;border:0px; "/>

Discover what no, this is the abstract attribute of all injected services, we convert it to Efcorecontext to initialize the data in this method, let's see.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

private static Efcorecontext context; public static void Initialize (IServiceProvider serviceprovider) {context = (Efcorecontext) serviceprovi Der.            GetService (typeof (Efcorecontext)); Do your Something}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Creating DbContext instances using generic dbcontextoptions

All of the above-described Dbcontextoptions objects are non-generic, as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

public class Efcorecontext:dbcontext {public efcorecontext (dbcontextoptions options): Base (options) {} protected override void Onmodelcreating (ModelBuilder ModelBuilder) {Modelbuilder.addentityconfigura Tionsfromassembly (GetType (). GetTypeInfo ().        Assembly); }    }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

But there is a generic version of the dbcontextoptions in its parameters, so what the generics are for, let's look at the following example:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

    public class efcorecontext1 : dbcontext    {         public efcorecontext1text1 (DbContextOptions< Efcorecontext1> options)             :  base (Options)         {         }    }    public class EFCoreContext2 :  DBCONTEXT&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;EFCORECONTEXT2 ( Dbcontextoptions<efcorecontext2> options)              : base (Options)         {         }    }    var contextOptions1  = new dbcontextoptionsbuilder&lT Efcorecontext1> ()         . Usesqlserver (connectionString1)         . options;    var contextoptions2 = new dbcontextoptionsbuilder< Efcorecontext2> ()         . Usesqlserver (connectionString2)         . Options;    var services = new servicecollection ()          . Addsingleton (contextOptions1)         . Addscoped<efcorecontext1> ()         . Addsingleton (CONTEXTOPTIONS2)         . Addscoped<efcorecontext2> ();     _serviceprovider = services. Buildserviceprovider ();

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

See what no, if there are multiple context types registered in the DI container, we can allow each context type to depend on its own options. When parsing EFCoreContext1 will cause dbcontextoptions<efcorecontext1> to be injected, and parsing EFCoreContext2 with due cause dbcontextoptions< Efcorecontext2> will be injected.

Use Adddbcontext to create DbContext instances

Register DbContext and Dbcontextoptions instances by adddbcontext syntax sugar. As follows:

var services = new Servicecollection (). Adddbcontext<efcorecontext> (b = b.usesqlserver (connectionString));

By default, Efcorecontext is registered as scope, and Dbcontextoptions is registered as a singleton, and you can change the Efcorecontext as a singleton registration, which we have already discussed. Okay, here we are. Presumably we know several ways to create an EF core context instance, which we summarize as the following three points.

(1) Call the context constructor directly and overload onconfiguring to create the context instance.

(2) Pass dbcontextoptions to the constructor to create the context instance.

(3) Create a context instance from the Di container.

Use Dbcontextoptionsbuilder to configure a dbcontext and eventually construct a Dbcontextoptions object, This can also be passed to the DbContext constructor by overloading the Onconfiguring method or by constructing the options, whether through a parameterless constructor or through dbcontextoptions to the context's constructor, or by injecting the dbcontext into the DI container is the same, there is no essential difference, but through di simple and forced lattice relatively high, no other.


How does entityframework Core 1.1 create an DbContext instance?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.