{"id":303078,"date":"2020-05-06T03:00:11","date_gmt":"2020-05-06T03:00:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=303078"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=303078","title":{"rendered":"Roslyn &amp; EF Core: \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u043c DbContext \u0432 runtime"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/500102\/\">\n<p>Entity Framework Core \u043c\u043e\u0436\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 DbContext \u0434\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>dotnet ef dbcontext scaffold<\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c DbContext \u0432 runtime?<\/p>\n<p>  <\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0432 runtime \u0432 \u0441\u0432\u043e\u0451\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 DbContext \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e EF Core.<\/li>\n<li>\u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Roslyn.<\/li>\n<li>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0441\u0431\u043e\u0440\u043a\u0443.<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e DbContext.<\/li>\n<li>\u0420\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 DbContext.<\/li>\n<\/ol>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/jdtcn\/RuntimeEfCore\" rel=\"nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 github.<\/a><\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"podgotovka-k-rabote\">\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043a \u0440\u0430\u0431\u043e\u0442\u0435<\/h1>\n<p>  <\/p>\n<p>\u041f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u043e\u0439 \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u0435\u0442 NET Core 3.1.3.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 MS SQL, \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u043d\u0435\u0439. \u041e\u0434\u043d\u0430\u043a\u043e, \u0441\u0430\u043c \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0434\u0432\u0438\u0436\u043a\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e EF Core (\u044f \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b sqlite \u0438 postregs).<\/p>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b:<\/p>\n<p>  <\/p>\n<pre><code class=\"xml\">&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;    &lt;PropertyGroup&gt;     &lt;OutputType&gt;Exe&lt;\/OutputType&gt;     &lt;TargetFramework&gt;netcoreapp3.1&lt;\/TargetFramework&gt;   &lt;\/PropertyGroup&gt;    &lt;ItemGroup&gt;     &lt;PackageReference Include=&quot;Microsoft.CodeAnalysis&quot; Version=&quot;3.5.0&quot; \/&gt;     &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore&quot; Version=&quot;3.1.3&quot; \/&gt;     &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;3.1.3&quot; \/&gt;     &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Proxies&quot; Version=&quot;3.1.3&quot; \/&gt;     &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer&quot; Version=&quot;3.1.3&quot; \/&gt;     &lt;PackageReference Include=&quot;Bricelam.EntityFrameworkCore.Pluralizer&quot; Version=&quot;1.0.0&quot; \/&gt;   &lt;\/ItemGroup&gt;  &lt;\/Project&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043a\u043e\u0434\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 <code>Microsoft.EntityFrameworkCore.Design<\/code>. \u0415\u0441\u043b\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0430\u043a\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 package manager console, \u0432 \u0432\u0430\u0448 *.csproj \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434: <\/p>\n<p>  <\/p>\n<pre><code class=\"xml\">&lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;3.1.3&quot;&gt;   &lt;PrivateAssets&gt;all&lt;\/PrivateAssets&gt;   &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers&lt;\/IncludeAssets&gt; &lt;\/PackageReference&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 [<a href=\"#ssylki\">1<\/a>], \u0447\u0442\u043e \u043f\u0430\u043a\u0435\u0442 \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 runtime. \u041d\u0430\u043c \u043e\u043d \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432 runtime, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0434\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"xml\">&lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;3.1.3&quot; \/&gt;<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"1-sgeneriruem-kod\">1. \u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0434<\/h1>\n<p>  <\/p>\n<p>\u0412 Entity Framework Core \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441 <code>IReverseEngineerScaffolder<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">interface IReverseEngineerScaffolder {     ScaffoldedModel ScaffoldModel(         string connectionString,           \/\/ \u0412\u044b\u0431\u043e\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0431\u0430\u0437\u044b         DatabaseModelFactoryOptions databaseOptions,           \/\/ \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439         ModelReverseEngineerOptions modelOptions,           \/\/ \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432 \u0438\u043c\u0451\u043d         ModelCodeGenerationOptions codeOptions); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u044d\u0442\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u0441\u043e\u0437\u0434\u0430\u0432 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0439 Dependency Injection Container. <\/p>\n<p>  <\/p>\n<p>\u0412 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">IReverseEngineerScaffolder CreateMssqlScaffolder() =&gt;      new ServiceCollection()         .AddEntityFrameworkSqlServer()         .AddLogging()         .AddEntityFrameworkDesignTimeServices()         .AddSingleton&lt;LoggingDefinitions, SqlServerLoggingDefinitions&gt;()         .AddSingleton&lt;IRelationalTypeMappingSource, SqlServerTypeMappingSource&gt;()         .AddSingleton&lt;IAnnotationCodeGenerator, AnnotationCodeGenerator&gt;()         .AddSingleton&lt;IDatabaseModelFactory, SqlServerDatabaseModelFactory&gt;()         .AddSingleton&lt;IProviderConfigurationCodeGenerator, SqlServerCodeGenerator&gt;()         .AddSingleton&lt;IScaffoldingModelFactory, RelationalScaffoldingModelFactory&gt;()         .AddSingleton&lt;IPluralizer, Bricelam.EntityFrameworkCore.Design.Pluralizer&gt;()         .BuildServiceProvider()         .GetRequiredService&lt;IReverseEngineerScaffolder&gt;(); <\/code><\/pre>\n<p>  <\/p>\n<p><code>IPluralizer<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0435\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u043d\u0430 \u0434\u043b\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0432\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">PostgreSQL<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">private IReverseEngineerScaffolder CreatePostgreScaffolder() =&gt;      new ServiceCollection()         .AddEntityFrameworkNpgsql()         .AddLogging()         .AddEntityFrameworkDesignTimeServices()         .AddSingleton&lt;LoggingDefinitions, NpgsqlLoggingDefinitions&gt;()         .AddSingleton&lt;IRelationalTypeMappingSource, NpgsqlTypeMappingSource&gt;()         .AddSingleton&lt;IAnnotationCodeGenerator, AnnotationCodeGenerator&gt;()         .AddSingleton&lt;IDatabaseModelFactory, NpgsqlDatabaseModelFactory&gt;()         .AddSingleton&lt;IProviderConfigurationCodeGenerator, NpgsqlCodeGenerator&gt;()         .AddSingleton&lt;IScaffoldingModelFactory, RelationalScaffoldingModelFactory&gt;()         .AddSingleton&lt;IPluralizer, Bricelam.EntityFrameworkCore.Design.Pluralizer&gt;()         .BuildServiceProvider()         .GetRequiredService&lt;IReverseEngineerScaffolder&gt;();<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">sqlite<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">private IReverseEngineerScaffolder CreateSqliteScaffolder() =&gt;      new ServiceCollection()         .AddEntityFrameworkSqlite()         .AddLogging()         .AddEntityFrameworkDesignTimeServices()         .AddSingleton&lt;LoggingDefinitions, SqliteLoggingDefinitions&gt;()         .AddSingleton&lt;IRelationalTypeMappingSource, SqliteTypeMappingSource&gt;()         .AddSingleton&lt;IAnnotationCodeGenerator, AnnotationCodeGenerator&gt;()         .AddSingleton&lt;IDatabaseModelFactory, SqliteDatabaseModelFactory&gt;()         .AddSingleton&lt;IProviderConfigurationCodeGenerator, SqliteCodeGenerator&gt;()         .AddSingleton&lt;IScaffoldingModelFactory, RelationalScaffoldingModelFactory&gt;()         .AddSingleton&lt;IPluralizer, Bricelam.EntityFrameworkCore.Design.Pluralizer&gt;()         .BuildServiceProvider()         .GetRequiredService&lt;IReverseEngineerScaffolder&gt;();<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043a\u043e\u0434\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">var scaffolder = CreateMssqlScaffolder();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">\/\/ \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432\u0441\u0435 \u0441\u0445\u0435\u043c\u044b \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b var dbOpts = new DatabaseModelFactoryOptions();  \/\/\u0418\u043c\u0435\u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043a\u0430\u043a \u0443 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 var modelOpts = new ModelReverseEngineerOptions();   var codeGenOpts = new ModelCodeGenerationOptions() {     \/\/ \u0417\u0430\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0438\u043c\u0451\u043d     RootNamespace = &quot;TypedDataContext&quot;,     ContextName = &quot;DataContext&quot;,     ContextNamespace = &quot;TypedDataContext.Context&quot;,     ModelNamespace = &quot;TypedDataContext.Models&quot;,      \/\/ \u041d\u0430\u0441 \u043d\u0435 \u043f\u0443\u0433\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435,     \/\/ \u0432\u0435\u0434\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 runtime     SuppressConnectionStringWarning = true };<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0441\u0451 \u0433\u043e\u0442\u043e\u0432\u043e, \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0434 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">ScaffoldedModel scaffoldedModelSources =         scaffolder.ScaffoldModel(\u0441onnectionString, dbOpts, modelOpts, codeGenOpts);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">\/\/ \u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c class ScaffoldedModel {     \/\/ \u041a\u043e\u0434 \u0444\u0430\u0439\u043b\u0430 DbContext     public virtual ScaffoldedFile ContextFile { get; set; }      \/\/ \u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441 \u043a\u043e\u0434\u043e\u043c \u043c\u043e\u0434\u0435\u043b\u0435\u0439     public virtual IList&lt;ScaffoldedFile&gt; AdditionalFiles { get; } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Lazy Loading, \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>UseLazyLoadingProxies()<\/code> \u0432 \u0444\u0430\u0439\u043b \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">var contextFile = scaffoldedModelSources.ContextFile.Code     .Replace(&quot;.UseSqlServer&quot;, &quot;.UseLazyLoadingProxies().UseSqlServer&quot;);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0433\u043e\u0442\u043e\u0432, \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e.<\/p>\n<p>  <\/p>\n<h1 id=\"2-kompiliruem-kod-s-pomoschyu-roslyn\">2. \u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Roslyn<\/h1>\n<p>  <\/p>\n<p>\u0421 Roslyn, \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">CSharpCompilation GenerateCode(List&lt;string&gt; sourceFiles) {     var options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8);     var parsedSyntaxTrees = sourceFiles         .Select(f =&gt; SyntaxFactory.ParseSyntaxTree(f, options));      return CSharpCompilation.Create($&quot;DataContext.dll&quot;,         parsedSyntaxTrees,         references: GetCompilationReferences(),         options: new CSharpCompilationOptions(             OutputKind.DynamicallyLinkedLibrary,             optimizationLevel: OptimizationLevel.Release)); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">List&lt;MetadataReference&gt; CompilationReferences() {     var refs = new List&lt;MetadataReference&gt;();      \/\/ \u0414\u043b\u044f \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u0438, \u0438 \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c\u0441\u044f \u0441\u043e\u0448\u043b\u0435\u043c\u0441\u044f \u043d\u0430 \u0432\u0441\u0435 \u0441\u0431\u043e\u0440\u043a\u0438,     \/\/ \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u044b \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0431\u043e\u0440\u043a\u0435      var referencedAssemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();     refs.AddRange(referencedAssemblies.Select(a =&gt;         MetadataReference.CreateFromFile(Assembly.Load(a).Location)));      \/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0441\u0431\u043e\u0440\u043a\u0438:     refs.Add(MetadataReference.CreateFromFile(         typeof(object).Assembly.Location));     refs.Add(MetadataReference.CreateFromFile(         Assembly.Load(&quot;netstandard, Version=2.0.0.0&quot;).Location));     refs.Add(MetadataReference.CreateFromFile(         typeof(System.Data.Common.DbConnection).Assembly.Location));     refs.Add(MetadataReference.CreateFromFile(         typeof(System.Linq.Expressions.Expression).Assembly.Location))      \/\/ \u0415\u0441\u043b\u0438 \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c LazyLoading, \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u0441\u0431\u043e\u0440\u043a\u0443:     \/\/ refs.Add(MetadataReference.CreateFromFile(     \/\/     typeof(ProxiesExtensions).Assembly.Location));      return refs; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u0444\u0430\u0439\u043b\u044b: <\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">MemoryStream peStream = new MemoryStream(); EmitResult emitResult = GenerateCode(sourceFiles).Emit(peStream);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0445\u0430, <code>emitResult.Success<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d <code>true<\/code>, \u0430 \u0432 <code>peStream<\/code> \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u043d\u0430\u0448\u0430 \u0441\u0431\u043e\u0440\u043a\u0430.<\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0439\u0434\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a, \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0433\u043a\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u0412 <code>emitResult<\/code> \u043f\u043e\u043f\u0430\u0434\u0443\u0442 \u0432\u0441\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<h1 id=\"3-zagruzhaem-sborku\">3. \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441\u0431\u043e\u0440\u043a\u0443<\/h1>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0441\u0431\u043e\u0440\u043a\u0430 \u0433\u043e\u0442\u043e\u0432\u0430, \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0435\u0451:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">var assemblyLoadContext = new AssemblyLoadContext(&quot;DbContext&quot;, isCollectible);  var assembly = assemblyLoadContext.LoadFromStream(peStream);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0425\u043e\u0447\u0443 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>isCollectible<\/code>. \u041e\u043d \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u0441\u0431\u043e\u0440\u043a\u0430 \u0431\u044b\u0442\u044c \u0432\u044b\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u0438 \u043e\u0447\u0438\u0449\u0435\u043d\u0430 \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u043e\u043c \u043c\u0443\u0441\u043e\u0440\u0430. \u042d\u0442\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0432 NET Core 3 [<a href=\"#ssylki\">2<\/a>].<\/p>\n<p>  <\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438, \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0431\u043e\u0440\u043a\u0443 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e [<a href=\"#ssylki\">5<\/a>]:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">assemblyLoadContext.Unload();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f LazyLoading, \u0442\u043e EF Core \u0431\u0443\u0434\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c Proxy-\u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e DefaultLoadContext, \u0430 \u043e\u043d \u043d\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d \u043a\u0430\u043a <code>collectible<\/code>. \u0422\u0430\u043a \u043a\u0430\u043a NonCollectible-\u0441\u0431\u043e\u0440\u043a\u0430 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 collectible-\u0441\u0431\u043e\u0440\u043a\u0443, \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0441\u0431\u043e\u0440\u043a\u0443 <code>collectible<\/code> \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c LazyLoading. \u041e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0435 \u0437\u043d\u0430\u044e\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 [<a href=\"#ssylki\">3<\/a>][<a href=\"#ssylki\">4<\/a>], \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u044d\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<h1 id=\"4-ispolzuem-nash-sgenerirovannyy-dbcontext\">4. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0430\u0448 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 DbContext<\/h1>\n<p>  <\/p>\n<p>\u041d\u0430\u0439\u0434\u0435\u043c \u0432 \u0441\u0431\u043e\u0440\u043a\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043d\u0430\u0448\u0435\u0433\u043e DbContext.<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">var type = assembly.GetType(&quot;TypedDataContext.Context.DataContext&quot;);  var constructor = type.GetConstructor(Type.EmptyTypes);  DbContext dynamicContext = (DbContext)constructor.Invoke(null);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static class DynamicContextExtensions {     public static IQueryable Query(this DbContext context, string entityName) =&gt;         context.Query(context.Model.FindEntityType(entityName).ClrType);      static readonly MethodInfo SetMethod =         typeof(DbContext).GetMethod(nameof(DbContext.Set), 1, Array.Empty&lt;Type&gt;()) ??         throw new Exception($&quot;Type not found: DbContext.Set&quot;);      public static IQueryable Query(this DbContext context, Type entityType) =&gt;         (IQueryable)SetMethod.MakeGenericMethod(entityType)?.Invoke(context, null) ??         throw new Exception($&quot;Type not found: {entityType.FullName}&quot;); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u0438\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Reflection \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0442\u0438\u043f\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443 <code>Set&lt;&gt;<\/code> \u043d\u0430\u0448\u0435\u0433\u043e DbContext.<\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">foreach (var entityType in dynamicContext.Model.GetEntityTypes()) {     var items = (IEnumerable&lt;object&gt;)dynamicContext.Query(entityType.Name);      Console.Write($&quot;Entity type: {entityType.ClrType.Name} &quot;);     Console.WriteLine($&quot;contains {items.Count()} items&quot;); }<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"scenarii-primeneniya\">\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f<\/h1>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0442\u0438\u043b\u0438\u0442 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0445\u0435\u043c\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0443\u0447\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043a\u043e\u0434\u0430.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 ASP.NET Core Blazor \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0445 \u0441 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c, \u0438\u043b\u0438 SQL, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MS SQL, PostrgreSQL, sqlite, \u0447\u0442\u043e\u0431\u044b<\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0421\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u0442\u0447\u0435\u0442\u044b \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"https:\/\/habrastorage.org\/webt\/0p\/yo\/o9\/0pyoo9f_l8cideknx0kyewnzgea.png\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/0p\/yo\/o9\/0pyoo9f_l8cideknx0kyewnzgea.png\"><\/a><\/p>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u0442\u0447\u0435\u0442\u044b<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"https:\/\/habrastorage.org\/webt\/ro\/jh\/8a\/rojh8a5_w2eucazndrqmklpzz8g.png\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ro\/jh\/8a\/rojh8a5_w2eucazndrqmklpzz8g.png\"><\/a><\/p>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0418 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0438\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"https:\/\/habrastorage.org\/webt\/ws\/rx\/ct\/wsrxctb4hdu1x0lpv6rvwteklh0.png\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ws\/rx\/ct\/wsrxctb4hdu1x0lpv6rvwteklh0.png\"><\/a><\/p>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0414\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u0442\u0447\u0435\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e c#<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"https:\/\/habrastorage.org\/webt\/fj\/e5\/2o\/fje52obtofiichnz-hnkhonk3z4.png\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fj\/e5\/2o\/fje52obtofiichnz-hnkhonk3z4.png\"><\/a><\/p>\n<\/div><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u0443 \u0431\u0430\u0437\u044b<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"https:\/\/habrastorage.org\/webt\/hh\/nr\/5x\/hhnr5xzyycnyl0vqccfmv2l-_64.png\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/hh\/nr\/5x\/hhnr5xzyycnyl0vqccfmv2l-_64.png\"><\/a><\/p>\n<\/div><\/div>\n<\/div><\/div>\n<p>  <\/p>\n<h1 id=\"vyvod\">\u0412\u044b\u0432\u043e\u0434<\/h1>\n<p>  <\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0434\u0430, \u043c\u043e\u0436\u043d\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c EF Core DbContext \u0432 runtime. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u043e\u0432\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 NET Core \u2014 <code>collectible assemblies<\/code>, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u0431\u043e\u0440\u043a\u0443 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u0435\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>  <\/p>\n<h1 id=\"ssylki\">\u0421\u0441\u044b\u043b\u043a\u0438<\/h1>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/jdtcn\/RuntimeEfCore\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 github.<\/a><\/p>\n<p>  <\/p>\n<p>[1] <a href=\"https:\/\/docs.microsoft.com\/ru-RU\/nuget\/consume-packages\/package-references-in-project-files\" rel=\"nofollow\">\u0421\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043f\u0430\u043a\u0435\u0442\u044b (PackageReference) \u0432 \u0444\u0430\u0439\u043b\u0430\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432<\/a><\/p>\n<p>  <\/p>\n<p>[2] <a href=\"https:\/\/www.strathweb.com\/2019\/01\/collectible-assemblies-in-net-core-3-0\/\" rel=\"nofollow\">Collectible assemblies in .NET Core 3.0<\/a><\/p>\n<p>  <\/p>\n<p>[3] <a href=\"https:\/\/github.com\/dotnet\/efcore\/issues\/18272\" rel=\"nofollow\">Lazy loading proxy doesn&#8217;t support entity inside collectible assembly #18272<\/a><\/p>\n<p>  <\/p>\n<p>[4] <a href=\"https:\/\/github.com\/castleproject\/Core\/issues\/473\" rel=\"nofollow\">Support Collectible Dynamic Assemblies #473<\/a><\/p>\n<p>  <\/p>\n<p>[5] <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/assembly\/unloadability\" rel=\"nofollow\">How to use and debug assembly unloadability in .NET Core<\/a><\/p>\n<p>  <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0443\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f!<\/p>\n<p>  <\/p>\n<p>\u0423 \u043c\u0435\u043d\u044f \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043c, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c. \u0411\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d, \u0435\u0441\u043b\u0438 \u0443\u043a\u0430\u0436\u0435\u0442\u0435 \u0432 \u043e\u043f\u0440\u043e\u0441\u0435 \u0442\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0431\u044b \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/500102\/\"> https:\/\/habr.com\/ru\/post\/500102\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/500102\/\">\n<p>Entity Framework Core \u043c\u043e\u0436\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 DbContext \u0434\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>dotnet ef dbcontext scaffold<\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c DbContext \u0432 runtime?<\/p>\n<p>  <\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0432 runtime \u0432 \u0441\u0432\u043e\u0451\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 DbContext \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e EF Core.<\/li>\n<li>\u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Roslyn.<\/li>\n<li>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0441\u0431\u043e\u0440\u043a\u0443.<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e DbContext.<\/li>\n<li>\u0420\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 DbContext.<\/li>\n<\/ol>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/jdtcn\/RuntimeEfCore\" rel=\"nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 github.<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-303078","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303078"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303078\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}