<commit_before>﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Entity Framework Core</title>
<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
</head>

<body>

<div class="document-contents">

<h3>Introduction</h3>
<p>
<a href="https://www.nuget.org/packages/Abp.EntityFrameworkCore" target="_blank">
Abp.EntityFrameworkCore</a> nuget package is used to integrate to Entity 
Framework (EF) Core ORM framework. After installing this package, we should also add 
a <a href="Module-System.html">DependsOn</a> attribute for <strong>
AbpEntityFrameworkCoreModule</strong>.</p>
	<h3>DbContext</h3>
	<p>EF Core requires to define a class derived from DbContext. In ABP, we 
	should derive from <strong>AbpDbContext</strong> as shown below:</p>
	<pre lang="cs">public class MyDbContext : <strong>AbpDbContext</strong>
{
    public DbSet&lt;Product&gt; Products { get; set; }

    public MyDbContext(<strong>DbContextOptions&lt;MyDbContext&gt; options</strong>)
        : base(options)
    {
    }
}</pre>
	<p>Constructor should get a <strong>DbContextOptions&lt;T&gt;</strong> as shown 
	above.</p>
	<h3>Configuration</h3>
	<p>There are a few ways of adding your DbContext to the application.</p>
	<h4>In Startup Class</h4>
	<p>For an ASP.NET Core project, you will have a <strong>Startup</strong> 
	file and you can use <strong>AddDbContext</strong> extension method of EF 
	Core as you always do:</p>
	<pre lang="cs">public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...
    
<strong>    services.AddDbContext&lt;MyDbContext&gt;(options =&gt;
    {
        options.UseSqlServer(Configuration.GetConnectionString(&quot;Default&quot;));
    });</strong>

    ...
}</pre>
	<p>This approach is standard, simple and enough for ABP. In this approach, 
	connection string is static (because this code runs once in the 
	application). If you have single database for a dbcontext, that's fine. But 
	if you want to dynamically determine connection string, see the next 
	section.</p>
	<h4>In Module PreInitialize</h4>
	<p>You can do it in PreInitialize of your module as shown below:</p>
	<pre lang="cs">public class MyEfCoreAppModule : AbpModule
{
    public override void PreInitialize()
    {
<strong>        Configuration.DefaultNameOrConnectionString = GetConnectionString(&quot;Default&quot;);

        Configuration.Modules.AbpEfCore().AddDbContext&lt;MyDbContext&gt;(options =&gt;
        {
            options.DbContextOptions.UseSqlServer(options.ConnectionString);
        });</strong>

        ...
    }
}</pre>
	<p>First, we are setting default connection string (we can get it from a 
	configuration file for example). Then we are using <strong>AddDbContext</strong> 
	method of ABP EF Core module. In this method, we can setup DbContext 
	options.</p>
	<p><strong>options.ConnectionString</strong> is the default connection 
	string normally. But since ABP uses IConnectionStringResolver to determine 
	it, this behaviour can be changed and connection string can be determined 
	dynamically. Also, in AddDbContext method, you don't have to use <strong>
	options.ConnectionString</strong> if you want.</p>
	<p>The action passed to AddDbContext is called whenever a DbContext instance 
	will be created.</p>
	<p>Note that; there is also IServiceCollection.<strong>AddAbpDbContext</strong> 
	extension method that can be used in Startup class. But even in that case, 
	you should define DefaultNameOrConnectionString in your module class.</p>

			<h3 id="DocRepositories">Repositories</h3>
			<p>Repositories are used to abstract data access from higher layers. 
			See <a href="Repositories.html">repository documentation</a> for 
			more.&nbsp;</p>
			<h4 id="DocImplRepositories">Default Repositories</h4>
			<p>
				<a href="http://www.nuget.org/packages/Abp.EntityFrameworkCore" target="_blank">Abp.EntityFrameworkCore</a> 
			implements default repositories for all entities defined in your 
			DbContext. You don't have to create repository classes to use 
predefined repository methods. Example:</p>
			<pre lang="cs">public class PersonAppService : IPersonAppService
{
    private readonly <strong>IRepository&lt;Person&gt; _personRepository</strong>;

    public PersonAppService(<strong>IRepository&lt;Person&gt; personRepository</strong>)
    {
        _personRepository = personRepository;
    }
<commit_msg>Updated EF Core document to configure DbContext in the Startup class by default.<commit_after>﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Entity Framework Core</title>
<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
</head>

<body>

<div class="document-contents">

<h3>Introduction</h3>
<p>
<a href="https://www.nuget.org/packages/Abp.EntityFrameworkCore" target="_blank">
Abp.EntityFrameworkCore</a> nuget package is used to integrate to Entity 
Framework (EF) Core ORM framework. After installing this package, we should also add 
a <a href="Module-System.html">DependsOn</a> attribute for <strong>
AbpEntityFrameworkCoreModule</strong>.</p>
	<h3>DbContext</h3>
	<p>EF Core requires to define a class derived from DbContext. In ABP, we 
	should derive from <strong>AbpDbContext</strong> as shown below:</p>
	<pre lang="cs">public class MyDbContext : <strong>AbpDbContext</strong>
{
    public DbSet&lt;Product&gt; Products { get; set; }

    public MyDbContext(<strong>DbContextOptions&lt;MyDbContext&gt; options</strong>)
        : base(options)
    {
    }
}</pre>
	<p>Constructor should get a <strong>DbContextOptions&lt;T&gt;</strong> as shown 
	above.</p>
	<h3>Configuration</h3>
	<h4>In Startup Class</h4>
	<p>For an ASP.NET Core project, you will have a <strong>Startup</strong> 
	file and you can use <strong>AddDbContext</strong> extension method of EF 
	Core as you always do:</p>
	<pre lang="cs">public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...
    
<strong>    services.AddDbContext&lt;MyDbContext&gt;(options =&gt;
    {
        options.UseSqlServer(Configuration.GetConnectionString(&quot;Default&quot;));
    });</strong>

    ...
}</pre>
	<p>This approach is standard, simple and enough for ABP. In this approach, 
	connection string is static (because we are always using the "Default" 
	connection string). If you have single database for a dbcontext, that's fine. But 
	if you want to dynamically determine connection string and better integrate 
	to ABP's connection string system, use <strong>AddAbpDbContext</strong> method:</p>
	<pre lang="cs">services.<strong>AddAbpDbContext</strong>&lt;MyDbContext&gt;(options =&gt;
{
    options.DbContextOptions.UseSqlServer(options.ConnectionString);
});</pre>
	<p>We used given connection string and used Sql Server as database provider. <strong>options.ConnectionString</strong> 
	is the <strong>default connection string</strong> (see next section) 
	normally. But since ABP uses IConnectionStringResolver to determine it. So, 
	this behaviour can be changed and connection string can be determined 
	dynamically. The action passed to AddDbContext is called whenever a DbContext instance 
	will be created. So, you also have a chance to return different connection 
	string conditionally.</p>
	<p>So, where to set default connection string?</p>
	<h4>In Module PreInitialize</h4>
	<p>You can do it in PreInitialize of your module as shown below:</p>
	<pre lang="cs">public class MyEfCoreAppModule : AbpModule
{
    public override void PreInitialize()
    {
<strong>        Configuration.DefaultNameOrConnectionString = GetConnectionString(&quot;Default&quot;);</strong>
        ...
    }
}</pre>
	<p>For non web projects, we will not have a Startup class. In that case, we 
	can use
<strong>        Configuration.Modules.AbpEfCore().AddDbContext</strong> method 
	here to configure DbContext, as shown below:</p>
	<pre lang="cs">Configuration.Modules.AbpEfCore().AddDbContext&lt;MyDbContext&gt;(options =&gt;
{
    options.DbContextOptions.UseSqlServer(options.ConnectionString);
});</pre>

			<h3 id="DocRepositories">Repositories</h3>
			<p>Repositories are used to abstract data access from higher layers. 
			See <a href="Repositories.html">repository documentation</a> for 
			more.&nbsp;</p>
			<h4 id="DocImplRepositories">Default Repositories</h4>
			<p>
				<a href="http://www.nuget.org/packages/Abp.EntityFrameworkCore" target="_blank">Abp.EntityFrameworkCore</a> 
			implements default repositories for all entities defined in your 
			DbContext. You don't have to create repository classes to use 
predefined repository methods. Example:</p>
			<pre lang="cs">public class PersonAppService : IPersonAppService
{
    private readonly <strong>IRepository&lt;Person&gt; _personRepository</strong>;

    public PersonAppService(<strong>IRepository&lt;Person&gt; personRepository</strong>)
    {
        _personRepository = personRepository;
    }

{'fraction_non_alphanumeric': 0.11893177640826036, 'fraction_numerical': 0.005622229430208671, 'mean_word_length': 4.6783302639656235, 'pattern_counts': {'":': 0, '<': 195, '<?xml version=': 0, '>': 195, 'https://': 2, 'lorem ipsum': 0, 'www.': 8, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4109095', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2769, 'n_words': 830}