私はそれに関する多くの記事を読みましたが、すべてが非常に複雑なソリューションのようです。しかし、私の問題を解決する簡単な方法があるはずです。
リソースからエラー メッセージを返す属性を使用して、オブジェクトの Entity Framework 検証を行いました (ただし、ErrorMessage = ... のように同じであることに問題はありません)。
[MetadataType(typeof(UserMetadata))]
public partial class User
{
internal sealed class UserMetadata
{
[Required(AllowEmptyStrings = false, ErrorMessageResourceName = "UserNameRequired", ErrorMessageResourceType = typeof(ErrorMessage))]
[StringLength(150, ErrorMessageResourceName = "UserNameLength", ErrorMessageResourceType = typeof(ErrorMessage))]
public string UserName { get; set; }
}
}
私のWCFサービスには、次の契約があります。
[ServiceContract]
public interface IUser
{
[OperationContract]
User AddUser(User user);
}
そして実装:
public class UserService: IUser
{
public User AddUser(User user)
{
//Here I think I should throw the ErrorMessage with a FaultException
//and to catch it in the client side, but how to do it !?
IUserRepository _user = new UserRepository(); //I've used EF Repository Pattern.
return _user.Add(user);
}
}