关于这个问题有很多问题,但我无法解决我的问题.

我有一个Office表,它与DoctorSecretary表有一个多关系.最后两个表都来自Employee表,Employee表与sqlmembershipprovider创建的预定义Users表具有共享主键关系.Users表和Roles表之间似乎有很多关系,我没有参与其中.

我的问题是在我的Employee表和Users表之间创建一个(0,1)-(1)关系,我在它们之间创建了一个共享主键关系,然后出现了错误.(有更好的解决方案吗?)

here is the error:

引入外键约束

here are my codes and membership codes after reverse engineering:

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; }
}

EDIT:,而且关系更深入,Users表和Applications表之间存在多个一的关系,Roles表和Applications表之间也存在多个一的关系.

推荐答案

可以使用fluent api指定错误消息建议的操作.

在你的背景下:

protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<aspnet_UsersInRoles>().HasMany(i => i.Users).WithRequired().WillCascadeOnDelete(false);
}

请注意,您尚未包含表aspnet_UsersInRoles的定义,因此此代码可能无法使用.

另一个选项是通过添加以下内容来删除所有级联删除

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

如果您需要更多关于使用Fluent API配置关系的信息,我建议您使用http://msdn.microsoft.com/en-US/data/jj591620

Asp.net相关问答推荐

在 AppConfig.json 中存储对象的最佳方式

如何从 JavaScript 调用 C# 函数?

jQuery隐藏字段

如何从 RouteData 获取路由名称?

HttpWebRequest 未通过凭据

使用 Elmah 处理 Web 服务中的异常

ASP.Net Core MVC - 自定义属性的客户端验证

我需要更改什么以允许我的 IIS7 ASP.Net 3.5 应用程序创建事件源并将事件记录到 Windows EventLog?

配置转换和TransformXml 任务失败错误消息

如何通过后面的代码不显示

返回 IHttpActionResult vs IEnumerable vs IQueryable

实体框架:如何解决外键约束可能导致循环或多个级联路径?

ASP.NET Core 中的 NuGet 包位置在哪里?

如何从 Web API 应用程序返回 PDF

如何在我自己的方法中模仿 string.Format()?

带有模型的 ASP.NET MVC 重定向

对于 DB ID,需要一个较小的 GUID 替代方案,但对于 URL 仍然是唯一且随机的

GZIP 与 DEFLATE 压缩相比有什么优势?

强制 IIS Express 进入classic 管道模式

Request.UserHostAddress 和 Request.ServerVariables["REMOTE_ADDR"].ToString() 有什么区别