Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

后端 未结 2 1043
我在风中等你
我在风中等你 2021-01-19 00:04

Hi I have the same problem that an old post that is here, the solution offer there doesn\'t work for me in MVC 6 with EF7 is simple

public class Match
{
            


        
2条回答
  •  难免孤独
    2021-01-19 00:33

    I analysed the problem in details during preparing the answer and I can suggest you two solutions of the problem.

    The problem exist because of two properties in the class Match

    public int HomeTeamId { get; set; }
    public int GuestTeamId { get; set; }
    

    and the foreign keys HomeTeamId and GuestTeamId which will be generated. EF7 generate the foreign keys with ON DELETE CASCADE, which can't be used for more as one foreign key. The current implementation of Entity Framework (RC1) have no annotation attribute, which you can use to change the behavior.

    The first solution of the problem would be to use nullable properties like

    public int? HomeTeamId { get; set; }
    public int? GuestTeamId { get; set; }
    

    or

    public int HomeTeamId { get; set; }
    public int? GuestTeamId { get; set; }
    

    Maximum one property should be non nullable. As the result the problem will be solved, but one will have small disadvantage, which could be not important for some scenarios. The field in the database table for nullable property will have no NOT NULL property in the column definition.

    If you do need to hold both HomeTeamId and GuestTeamId non-nullable then you can solve the problem by modifying the context class (inherited from DbContext), where the classes Match and Team be used.

    You have already some context class defined line below

    public class MyDBContext : DbContext
    {
        DbSet Teams { get; set; }
        DbSet Matches { get; set; }
    }
    

    To solve the describe problem you can add protected OnModelCreating in the class which explicitly set

    public class MyDBContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelbuilder)
        {
            base.OnModelCreating(modelbuilder);
    
            modelbuilder.Entity(typeof (Match))
                .HasOne(typeof (Team), "GuestTeam")
                .WithMany()
                .HasForeignKey("GuestTeamId")
                .OnDelete(DeleteBehavior.Restrict); // no ON DELETE
    
            modelbuilder.Entity(typeof (Match))
                .HasOne(typeof (Team), "HomeTeam")
                .WithMany()
                .HasForeignKey("GuestTeamId")
                .OnDelete(DeleteBehavior.Cascade); // set ON DELETE CASCADE
        }
    
        DbSet Teams { get; set; }
        DbSet Matches { get; set; }
    }
    

    You can use of cause DeleteBehavior.Restrict on both foreign keys (instead of usage of DeleteBehavior.Cascade on one). It's important to remark that the last approach allows to hold both HomeTeamId and GuestTeamId, like the corresponding fields in the database, as non-nullable.

    See the documentation for additional information.

提交回复
热议问题