[英]Entity Framework IsUnique and accept Nulls
How can I make a Code First property that accepts null values but it's also an [Index(IsUnique=true)]
? 我怎样才能使Code First属性接受空值,但它也是
[Index(IsUnique=true)]
?
For example: a passport number should't be repeated, ergo the IsUnique
, but if the user doesn't insert one, then it defaults to null, so it accepts multiple nulls. 例如:护照号码不应该重复,
IsUnique
,但是如果用户不输入护照号码,则默认为null,因此可以接受多个null。
Since it will not be unique then, you cannot do it that way. 由于它不是唯一的,所以您不能那样做。 But you can manage it, by attaching a table:
但是您可以通过附加表来管理它:
public class Person {
public int Id { get; set; }
}
public class Passport {
[Key]
public int PersonId { get; set; }
[ForeignKey("PersonId")]
public Person Person { get; set; }
[Index(IsUnique=true)]
public string PassportId { get; set; }
}
Be aware, that there are countries, which do not allow storing such numbers. 请注意,有些国家不允许存储此类数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.