プロジェクトで O/R マッピングに Entity Framework を使用し、検証に DataAnnotations を使用することにしましたが、これを実装しようとすると奇妙な問題が発生しました。
これは私がやったことです:
次のエンティティタイプがあります
Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null)
Address Address (not null)
String Phone
String Email
ここでName
、 とAddress
は次のように定義された複合型です。
Name Address
**** *******
String First (not null) String Street (not null)
String Last (not null) String ZipCode (not null)
String City (not null)
そして、次のクラスは私のエンティティと同じ名前空間に存在します:
public class ContactMetadata
{
[Required]
public Name Name { get; set; }
}
[MetadataType(typeof(ContactMetadata))]
partial class Contact { }
ただし、新しいContact
アイテムを作成すると、Name
andの型は、値自体を持つandではなく、すべての値がであるandAddress
のインスタンスで満たされます。したがって、すべての値が であっても、属性はエラーをスローしません。これを回避するにはどうすればよいですか?Name
Address
null
Name
Address
null
Required
null