简体   繁体   中英

C# Entity Framework 6 foreign key constraint in an unexpected format being generated

I am adding 2 models which contains a foreign key. When analysing the query generated, EF is generating the constraint name in an unexpected manner.

  public class SnapAppSession
{
    [Key]
    public int Id { get; set; }


    [MaxLength(36)]
    public string AppUploadSessionHandle { get; set; }

    [ForeignKey("User")]
    public string UserId { get; set; }

    public DateTime CreatedOn { get; set; }


    public virtual ApplicationUser User { get; set; }


    public virtual ICollection<SnapAppSessionsBreakdown> Breakdown { get; set; }

}



public class SnapAppSessionsBreakdown
{
    [Key]
    public int Id { get; set; }


    [ForeignKey("SnapAppSession")]
    public int SnapAppSessionId { get; set; }

    public string FileName { get; set; }

    public virtual SnapAppSession SnapAppSession { get; set; }


}

The foreign key constraint query that is being generated is: SnapAppSession - (expected name FK_SnapAppSessions_AspNetUsers_UserId):

alter table `SnapAppSessions` add constraint `FK_SnapAppSessions_AspNetUsers_UserId`  foreign key (`UserId`) references `AspNetUsers` ( `Id`) 

SnapAppSessionsBreakdowns - (unexpected name of FK_4342715c7ab44a358f96e48e0cce132e):

alter table `SnapAppSessionsBreakdowns` add constraint `FK_4342715c7ab44a358f96e48e0cce132e`  foreign key (`SnapAppSessionId`) references `SnapAppSessions` ( `Id`) 

Any reason why the name being generated is in a random manner? I am using MYSQL EF if that makes any difference.

此行为与生成的 FK 名称长度超过特定限制有关,如MySQL 错误 #74726 中所述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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