我有一个实体,该实体的表有两个外键。看起来是这样的:
public class SystemUser : BaseModel
{
[Column("SystemUserId")]
public override Guid ID { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string MobilePhone { get; set; }
public Guid? MobilePhoneProviderId { get; set; }
public virtual MobilePhoneProvider MobilePhoneProvider { get; set; }
public Guid? ManagerId { get; set; }
public virtual SystemUser Manager { get; set; }
public Guid? AdminAssistantId { get; set; }
public virtual SystemUser AdminAssistant { get; set; }
public bool IsActive { get; set; }
public bool SendEmail { get; set; }
public string FinaleUsername { get; set; }
public string FinalePassword { get; set; }
public int? FloatUserId { get; set; }
public Guid? SystemUserTypeId { get; set; }
public virtual SystemUserType SystemUserType { get; set; }
public Guid? SystemUserJobFunctionId { get; set; }
public virtual SystemUserJobFunction SystemUserJobFunction { get; set; }
public Guid? SystemUserJobRoleId { get; set; }
public virtual SystemUserJobRole SystemUserJobRole { get; set; }
public Guid? SystemUserLocationId { get; set; }
public virtual SystemUserLocation SystemUserLocation { get; set; }
public virtual ICollection<SystemUserRole> Roles { get; set; }
}
注意AdminAssistant
和Manager
属性。但是,这给了我以下错误:
无法确定类型'SystemUser'和'SystemUser'之间的关联的主要终点。必须使用关系流利的API或数据注释显式配置此关联的主要端。
如果我删除其中的一个(无论哪个,都没有关系),它都可以正常工作,但是由于某种原因,对于同时拥有它们,我感到困惑。我尝试ForeignKey
在每个属性上添加属性,以及使用流畅的API进行如下配置:
modelBuilder.Entity<SystemUser>()
.HasOptional(x => x.Manager)
.WithMany()
.HasForeignKey(x => x.ManagerId);
modelBuilder.Entity<SystemUser>()
.HasOptional(x => x.AdminAssistant)
.WithMany()
.HasForeignKey(x => x.AdminAssistantId);
两者都不起作用。有什么办法可以做到吗?
仅供参考,我没有使用Code First。我正在手动编写数据库脚本,仅使用EF作为ORM。不知道该信息是否相关,但我想还是会给它。
看起来您会想要包含类似
ICollection<SystemUser> ManagedUsers {get; set;}
ICollection<SystemUser> AdminAssistedUsers {get; set;}
然后,您应该可以执行以下操作:
modelBuilder.Entity<SystemUser>()
.HasOptional(x => x.Manager)
.WithMany(x => x.ManagedUsers)
.HasForeignKey(x => x.ManagerId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<SystemUser>()
.HasOptional(x => x.AdminAssistant)
.WithMany(x => x.AdminAssistedUsers)
.HasForeignKey(x => x.AdminAssistantId)
.WillCascadeOnDelete(false);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句