我有以下两个班级:
public class UserMembership: Entity
{
public Guid Id { get; set; }
public Guid UserGroupId { get; set; }
public string LoginId { get; set; }
public Guid SiteId { get; set; }
public virtual Site Site { get; set; }
public virtual SecurityUser SecurityUser { get; set; }
}
和
public class SecurityUser: Entity
{
ICollection<UserMembership> _userMemberships = new Collection<UserMembership>();
public Guid Id { get; set; }
public string LoginId { get; set; }
public string DisplayName { get; set; }
public DateTimeOffset? LastLogOn { get; set; }
public bool IsActive { get; set; }
public bool IsServiceAccount { get; set; }
public bool IsUserAccount { get; set; }
public virtual ICollection<UserMembership> UserMemberships
{
get { return _userMemberships; }
set { _userMemberships = value; }
}
}
想要使用LoginId字段而不是主键加入UserMembership和SecurityUser。这可能是使用Fluent API而不是属性吗?
以下是映射类:
public class SecurityUserMap : EntityMap<SecurityUser>
{
public SecurityUserMap()
{
HasKey(p => p.Id);
Property(p => p.LoginId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute{ IsClustered = true, IsUnique = true }))
.HasMaxLength(SecurityUserValidator.MaxLoginIdLength).IsRequired();
Property(p => p.DisplayName)
.HasMaxLength(SecurityUserValidator.MaxDisplayNameLength).IsRequired();
Property(p => p.Modified)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));
}
}
和
public class UserMembershipMap: EntityMap<UserMembership>
{
public UserMembershipMap()
{
HasKey(p => p.Id);
Property(p => p.UserGroupId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UK_UserMembership", 1) { IsClustered = true, IsUnique = true }));
Property(p => p.LoginId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UK_UserMembership", 2) { IsClustered = true, IsUnique = true }))
.HasMaxLength(UserMembershipValidator.MaxLoginIdLength).IsRequired();
Property(p => p.SiteId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UK_UserMembership", 3) { IsClustered = true, IsUnique = true }));
Property(p => p.Modified)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));
HasRequired(p => p.SecurityUser).WithMany(p => p.UserMemberships).Map(t => t.MapKey(new[] { "LoginId" }));
}
}