模型生成过程中检测到一个或多个验证错误:
OMS.Repository.Context.Customer_BelongTo: : 多重性与关系“Customer_BelongTo”中 Role“Customer_BelongTo_Target”中的引用约束冲突。因为 Dependent Role 中的所有属性都不可以为 null,Principal Role 的多重性必须为“1”。
这个是什么原因,怎么解决?
public class Customer : BaseEntity { public string Code { get; set; } public string Name { get; set; } public int RankId { get; set; } //其他属性略 /// <summary> /// 归属人,可以为空 /// </summary> public Login BelongTo { get; set; } public int BelongToUserId { get; set; } }
public class Login : BaseEntity, IPrincipal { public Login() { Roles = new List<Role>(); } public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public bool Enabled { get; set; } public virtual User UserInfo { get; set; } public virtual ICollection<Role> Roles { get; set; } public IIdentity Identity { get { var isAuthenticated = Id > 0; return new Identity(isAuthenticated, UserName); } } public bool IsInRole(string role) { return false; } public virtual Organization Organization { get; set; } /// <summary> /// 全部权限 /// </summary> public IList<UnitPermission> UnitPermissions { get { var permissions = new List<UnitPermission>(); foreach (var role in Roles.Where(f => f.Enabled)) { permissions.AddRange(role.UnitPermissions); } return permissions.Distinct().ToList(); } } }
public class CustomerCinfig : EntityMapperBase<Customer> { public CustomerCinfig() { HasKey(f => f.Id); Property(f => f.Code).HasMaxLength(20).IsRequired(); Property(f => f.Name).HasMaxLength(40).IsRequired(); HasRequired(f => f.Rank).WithMany().HasForeignKey(f => f.RankId).WillCascadeOnDelete(false); HasOptional(f => f.BelongTo).WithMany().HasForeignKey(f => f.BelongToUserId); } }
HasOptional(f => f.BelongTo). 改成 HasRequired
但是这个字段并不是必填项额
貌似是 public int BelongToUserId { get; set; } 的问题
改成 public int? BelongToUserId { get; set; } 就好了。。。 冏