关于这个问题有很多问题,但我无法解决我的问题。有人可以看看这个:
我有一张与和表Office
有一对多关系的表。最后两个表都派生自与创建的预定义表具有共享主键关系的表。似乎table 和table之间存在多对多的关系,我没有参与其中。Doctor
Secretary
Employee
Users
sqlmembershipprovider
Users
Roles
Employee
我的问题是在我的表和该表之间创建(零,一)-(一)关系,Users
我以它们之间的共享主键关系结束,然后引发错误。(这个问题有更好的解决方案吗?)
这是错误:
在表“aspnet_UsersInRoles”上引入 FOREIGN KEY 约束“FK_dbo.aspnet_UsersInRoles_dbo.aspnet_Users_UserId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束。请参阅以前的错误。
这是我在逆向工程后的代码和会员代码:
public class Office
{
public Office()
{
this.Doctors = new HashSet<Doctor>();
this.Secretaries = new HashSet<Secretary>();
}
[Key]
public System.Guid OfficeId { get; set; }
public virtual ICollection<Doctor> Doctors { get; set; }
public virtual ICollection<Secretary> Secretaries { get; set; }
}
public class Employee
{
[Key, ForeignKey("User")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public System.Guid Id { get; set; }
public string Name { get; set; }
[ForeignKey("Office")]
public System.Guid OfficeId { get; set; }
// shared primary key
public virtual aspnet_Users User { get; set; }
public virtual Office Office { get; set; }
}
public class Doctor :Employee
{
public Doctor()
{
this.Expertises = new HashSet<Expertise>();
}
//the rest..
public virtual ICollection<Expertise> Expertises { get; set; }
}
public class Secretary : Employee
{
// blah blah
}
public class aspnet_Users
{
public aspnet_Users()
{
this.aspnet_Roles = new List<aspnet_Roles>();
}
public System.Guid ApplicationId { get; set; }
public System.Guid UserId { get; set; }
//the rest..
public virtual aspnet_Applications aspnet_Applications { get; set; }
public virtual ICollection<aspnet_Roles> aspnet_Roles { get; set; }
}
public class aspnet_Roles
{
public aspnet_Roles()
{
this.aspnet_Users = new List<aspnet_Users>();
}
public System.Guid ApplicationId { get; set; }
public System.Guid RoleId { get; set; }
//the rest..
public virtual aspnet_Applications aspnet_Applications { get; set; }
public virtual ICollection<aspnet_Users> aspnet_Users { get; set; }
}
编辑:Users
关系更深入,表和表之间存在多对一的关系Applications
,也存在于Roles
和之间Applications
。