2

ValueInjecter を使用して、ビュー モデルを Entity Framework (4.3.1) モデル ファーストで作成されたドメイン オブジェクトにフラット化/非フラット化しています。VARCHAR私のデータベースにある私のコラムはすべてNOT NULL DEFAULT ''(個人的な好みであり、ここで聖戦を開始するつもりはありません) です。投稿時に、値のない文字列プロパティが null としてビュー モデルに返されるため、それをドメイン モデル クラスに挿入しようとすると、プロパティIsNullable=falseを null に設定しようとして EF が吠えます。例(単純すぎる):

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
    }
}

次に、コントローラーの投稿は次のようになります。

[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の実際のドメイン バージョンにネストされた型があるため、使用していますThingConventionInjectionnull 文字列を に変換するカスタムを作成しようとしましたstring.Emptyが、UnflatLoopValueInjectionnull に戻すようです。これを行わないように ValueInjecter を取得する方法はありますか?

4

1 に答える 1

1

ナッツ、私はwikiの助けを借りてそれを理解しました。解決策は次のように拡張することUnflatLoopValueInjectionです:

public class NullStringUnflatLoopValueInjection : UnflatLoopValueInjection<string, string>
{
    protected override string SetValue(string sourceValue)
    {
        return sourceValue ?? string.Empty;
    }
}
于 2012-05-08T17:16:25.407 に答える