【发布时间】:2013-01-07 12:29:15
【问题描述】:
关于这个问题有很多问题,但我无法解决我的问题。 有人可以看看这个吗:
我有一个Office 表,它与Doctor 和Secretary 表有一对多的关系。最后两个表都派生自Employee 表,该表与sqlmembershipprovider 创建的预定义Users 表具有共享主键关系。 Users table 和 Roles table 之间似乎存在多对多关系,我没有参与其中。
我的问题是在我的Employee 表和Users 表之间创建(零,一)-(一)关系,我以它们之间的共享主键关系结束,然后引发错误。 (这个问题有更好的解决方案吗?)
这是错误:
引入 FOREIGN KEY 约束 表上的“FK_dbo.aspnet_UsersInRoles_dbo.aspnet_Users_UserId” “aspnet_UsersInRoles”可能会导致循环或多个级联路径。 指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 外键约束。无法创建约束。见上 错误。
这是我在逆向工程后的代码和会员代码:
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 之间也存在。
【问题讨论】:
-
让我说对了,您是否对 Employee 和 Users 表中的 PK 使用相同的值?
-
是的,因为我希望首先应该有一个用户价值,然后我在 Employee 中使用它。该错误仅在初始化时引发。
标签: asp.net entity-framework sqlmembershipprovider