使用UniqueConstraints捆绑包:
public class Person
{
public string Name {get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string GoogleAndFacebookIds { get;set; }
}
只要确保GoogleAndFacebookIds
每次更新GoogleId
或更新时都更新FacebookId
。我做了这么多,我最终在我所有的类上使用了一个简单的接口来做这种事情:
public interface ICombinedConstraints
{
string UniqueId { get; set; }
void UpdateConstraints();
}
所以,
public class Person : ICombinedConstraints
{
public string Name{get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string UniqueId { get; set; }
public void UpdateConstraints()
{
UniqueId = GoogleId + FacebookId;
}
}