2012-05-08 3 views
2

ValueInjecter를 사용하여 뷰 모델을 Entity Framework (4.3.1)에서 먼저 작성한 도메인 개체로 플랫 패널/플랫 플레이트 해제합니다. 내 데이터베이스에있는 내 VARCHAR 열은 모두 NOT NULL DEFAULT '' (개인적 취향, 여기에 성전을 열어 보려는 의도는 없음)입니다. 게시물에서 뷰 모델은 null 값이없는 문자열 속성으로 돌아옵니다. 따라서 도메인 모델 클래스로 다시 주입하려고 시도 할 때 EF는 IsNullable=false으로 속성을 null로 설정하려고 시도한 것에 대해 저를 경계합니다. 예 (이상 단순) :ValueInjecter를 사용하여 null 문자열을 string.Empty로 변경합니다.

public class ThingViewModel 
{ 
    public int ThingId{get;set;} 
    public string Name{get;set;} 
} 

public class Thing 
{ 
    public global::System.Int32 ThingId 
    { 
     //omitted for brevity 
    } 

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Name 
    { 
     //omitted for brevity 
    } 
} 

그런 다음 내 컨트롤러 포스트는 다음과 같습니다 : 나는 Thing의 실제 도메인 버전의 유형을 중첩 때문에

[HttpPost] 
public ActionResult Edit(ThingViewModel thing) 
{ 
    var dbThing = _thingRepo.GetThing(thing.ThingId); 
    //if thing.Name is null, this bombs 
    dbThing.InjectFrom<UnflatLoopValueInjection>(thing); 
    _thingRepo.Save(); 
    return View(thing); 
} 

나는 UnflatLoopValueInjection을 사용하고 있습니다. null 문자열을 string.Empty으로 변환하기 위해 사용자 정의 ConventionInjection을 작성하려고했지만 UnflatLoopValueInjection이 null로 다시 전환됩니다. ValueInjecter가이 작업을 수행하지 못하게 할 수있는 방법이 있습니까?

+0

모델 우선 또는 코드 우선? Model-First 또는 DB-First 인 경우 사용자 정의 T4를 사용하고 있습니까? 그렇다면 Code-First를 사용하고있는 경우 속성 확인자에 null 체크를 추가 할 수 있습니다. –

+0

@DannyVarod 모델 - 처음에는 질문에 언급 된대로 맞춤 T4를 사용하지 않습니다. 나는'TargetProp'의 속성을 반영하려고 시도했지만 그렇게하지 않았다. –

+0

일부 사람들은이 두 가지를 혼동합니다. 기본 생성기 T4를 사용하는 경우 엔티티에 기본 클래스가 있어야합니다. –

답변

1

견과류, 나는 방금 wiki에서 도움을 얻어 냈습니다. 해결책은 연장 될 것으로 보인다. UnflatLoopValueInjection :

public class NullStringUnflatLoopValueInjection : UnflatLoopValueInjection<string, string> 
{ 
    protected override string SetValue(string sourceValue) 
    { 
     return sourceValue ?? string.Empty; 
    } 
} 
관련 문제