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