简体   繁体   English

Entity Framework 4.1 InverseProperty 属性和ForeignKey

[英]Entity Framework 4.1 InverseProperty Attribute and ForeignKey

I will create two references between Employee and Team entities with foreign keys.我将使用外键在 Employee 和 Team 实体之间创建两个引用。 So I defined two entities as follow所以我定义了两个实体如下

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }

    [ForeignKey("FirstTeam")]
    public int FirstTeamId { get; set; }

    [InverseProperty("FirstEmployees")]
    public virtual Team FirstTeam { get; set; }

    [ForeignKey("SecondTeam")]
    public int SecondTeamId { get; set; }

    [InverseProperty("SecondEmployees")]
    public virtual Team SecondTeam { get; set; }
}

public class Team
{
    public int Id { get; set; }
    public string TeamName { get; set; }

    [InverseProperty("FirstTeam")]
    public virtual ICollection<Employee> FirstEmployees { get; set; }

    [InverseProperty("SecondTeam")]
    public virtual ICollection<Employee> SecondEmployees { get; set; }
}

I thought it is correct theoretically, but it shows the Exception as follow:我认为理论上是正确的,但它显示异常如下:

{"Introducing FOREIGN KEY constraint 'Employee_SecondTeam' on table 'Employees' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors."}

Can anybody help me?有谁能够帮我?

Thanks in advance Kwon预先感谢权

It is theoretically correct but SQL server (not Entity framework) doesn't like it because your model allows single employee to be a member of both First and Second team.理论上是正确的,但 SQL 服务器(不是实体框架)不喜欢它,因为您的 model 允许单个员工同时成为第一和第二团队的成员。 If the Team is deleted this will cause multiple delete paths to the same Employee entity.如果Team被删除,这将导致同一Employee实体的多个删除路径。

This cannot be used together with cascade deletes which are used by default in EF code first if you define foreign key as mandatory (not nullable).如果您将外键定义为强制(不可为空),则这不能与默认情况下在 EF 代码中首先使用的级联删除一起使用。

If you want to avoid the exception you must use fluent mapping:如果要避免异常,则必须使用流畅的映射:

public Context : DbContext
{
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Team> Teams { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Employee>()
                    .HasRequired(e => e.SecondTeam)
                    .WithMany(t => t.SecondEmployees)
                    .HasForeignKey(e => e.FirstTeamId)
                    .WillCascadeOnDelete(false);

        ...
    }
}

This will result in scenario where you must delete members of SecondTeam manually before you delete the team.这将导致您必须在删除团队之前手动删除 SecondTeam 的成员。

All is correct in previous answer, but one thing is wrong上一个答案都是正确的,但有一件事是错误的

    modelBuilder.Entity<Employee>()
                .HasRequired(e => e.SecondTeam)
                .WithMany(t => t.SecondEmployees)
                .HasForeignKey(e => e.SecondTeamId) // mistake
                .WillCascadeOnDelete(false);

FirstTeamId instead of SecondTeamId will cause that in SecondTeam navigation property will be always FirstTeam FirstTeamId而不是SecondTeamId将导致在SecondTeam navigation属性将始终FirstTeam

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM