Programing

먼저 EF 코드의 CTP5에 대해 ProxyCreationEnabled를 끄는 단점은 무엇입니까?

lottogame 2020. 10. 10. 09:31
반응형

먼저 EF 코드의 CTP5에 대해 ProxyCreationEnabled를 끄는 단점은 무엇입니까?


내 WCF 서비스가 코드 우선 모델에서 클래스를 반환 할 수있는 유일한 방법은 아래 코드 ProxyCreationEnablefalse사용하여를 로 설정하는 입니다.

((IObjectContextAdapter)MyDb).ObjectContext.ContextOptions.ProxyCreationEnable = false;

이렇게하면 부정적인 결과는 무엇입니까? 한 가지 이점은 최소한 이러한 동적 유형을 직렬화하여 WCF를 사용하여 유선으로 전송할 수 있다는 것입니다.


동적 프록시는 변경 추적 및 지연로드에 사용됩니다. WCF가 개체를 직렬화하려고 할 때 관련 컨텍스트는 일반적으로 닫히고 삭제되지만 탐색 속성의 직렬화는 자동으로 지연로드 (닫힌 컨텍스트에서) => 예외를 트리거합니다.

지연로드를 끄면 사용하려는 모든 탐색 속성에 대해 즉시로드를 사용해야합니다 (ObjectQuery에 포함). 변경 내용 추적은 WCF에서 작동하지 않으며 ObjectContext에 연결된 엔터티를 수정하는 경우에만 작동합니다.


가로 DbContext.Configuration.ProxyCreationEnabled설정 되면 false부모 개체에서 Include메서드가 호출 되지 않는 한 DbContext는 일부 부모 개체에 대한 자식 개체를로드하지 않습니다 . 또는로 설정 DbContext.Configuration.LazyLoadingEnabled하면 동작에 영향을주지 않습니다.truefalse

경우 DbContext.Configuration.ProxyCreationEnabled로 설정되어 true, 자식 개체는 자동으로로드되며, DbContext.Configuration.LazyLoadingEnabled자식 개체가로드 될 때 값을 제어합니다.


EF를 사용하면 기본적으로 클래스에 대한 프록시가 생성됩니다. 해결책은 DbContext 클래스의 생성자에이 줄을 추가하는 것입니다. DbContext 클래스에서 상속 된 데이터 모델이므로 다음과 같이 모델을 편집 할 수 있습니다.

    public yourDataModelEntities()
        : base("name=yourDataModelEntities")
    {
        base.Configuration.ProxyCreationEnabled = false;
    }

이 클래스는 당신에 EF.edmx에서 다음 yourmodel.Context.tt다음yourmodel.Context.cs


(Visual Studio 2013 이상 사용)

데이터베이스에서 모델을 새로 고칠 때마다 EF 모델에서 클래스 생성자의 편집을 방지하거나 다른 방법으로 코드 재 빌드를 트리거하려면 변경을 수행하는 적절한 위치는 다음을 담당하는 T4 코드 파일입니다. 실제로 모델 코드를 생성합니다. 몇 년 전 클래스와 속성이 실제로 어떻게 생성되었는지에 대한 기본 메커니즘을 이해했을 때 동적 속성과 관련된 다른 문제가있었습니다. T4 !!! 그것이 얼마나 기적인가 :-D T4 구문은 처음에는 약간 위협적 일 수 있으므로 구문을 읽는 것이 현명합니다. 변경할 때 매우 집중하는 것도 좋은 생각입니다 :-)

그래서! 모델을 보면 .edmx 파일 아래에 .tt 파일이 있습니다. 이 .tt (T4) 파일은 실제로 모델 클래스를 생성하는 스크립트입니다. 스크립트는 모델을 빌드하거나 모델 편집기에서 일부를 변경할 때마다 자동으로 실행됩니다.

모델 설명 자의 이름이 Model1.edmx 라고 가정 해 보겠습니다 . 그 아래의 트리에 Model1.Context.tt 라는 파일이 있습니다 . Model1.Context.cs 파일 도 볼 수 있습니다. 이것은 분명히 귀하의 컨텍스트에 대한 실제 코드 파일입니다. 그러나이 파일은 .tt 스크립트 파일이 실행 된 결과입니다 ! 완전히 동적으로 생성됩니다. 그래서 그것을 편집 할 생각이 없습니다.

.tt 파일을 열면 다음과 같은 내용이 표시됩니다.

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF6.Utility.CS.ttinclude"#><#@
 output extension=".cs"#><#

const string inputFile = @"Model1.edmx";
var textTransform = DynamicTextTransformation.Create(this);
..
..

50 개 정도의 줄이 추가되면 생성자 코드가 스크립팅됩니다.

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
<#
if (container.FunctionImports.Any())
{
#>
using System.Data.Entity.Core.Objects;
using System.Linq;
<#
}
#>

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
    {
        public <#=code.Escape(container)#>()
            : base("name=<#=container.Name#>")
        {
        base.Configuration.ProxyCreationEnabled = false;
    <#
    if (!loader.IsLazyLoadingEnabled(container))
    {
    #>
            this.Configuration.LazyLoadingEnabled = false;
    <#
    }

I have added the property base.Configuration.ProxyCreationEnabled = false; so that it will be the very first line in the constructor.

Save your file, and open the Model1.Context.cs file to see the resulting code. If you want to force the template script to run, select the menu

Build - Tranform all T4 templates

It is easy to know if you've made a mistake in your T4 code, as the .cs file will be either not made at all, or with obvious errors if you open it in the editor.

참고URL : https://stackoverflow.com/questions/4596371/what-are-the-downsides-to-turning-off-proxycreationenabled-for-ctp5-of-ef-code-f

반응형