Base solution for your next web application
Open Closed

Add Foreign key in existing database tables using EF Core #6529


User avatar
0
kalidarscope created

After migration, update-databse -verbose throwing the below error

To change the IDENTITY property of a column, the column needs to be dropped and recreated.

[Table("MMTABS")]
public class ASAPTabsEntity : FullAuditedEntity
{
[ForeignKey("Id")]
public virtual ASAPModulesEntity ASAPModulesEntities { get; set; }
public virtual int? PARENT_MODULE_ID { get; set; }
}

 [Table("MMMODL")]
public class ASAPModulesEntity : FullAuditedEntity
{
       public virtual ICollection<ASAPTabsEntity> ASAPTabsEntities { get; set; }
}

here, [ForeignKey("Id")] it is not the PK of the MMTABS table, as it is PK of the MMMODL table trying to create one to many relationship with MMMODL and MMTABS

hence I have written the above


12 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Show code.

  • User Avatar
    0
    aaron created
    Support Team

    Why are you setting the primary key as the foreign key?

  • User Avatar
    0
    kalidarscope created

    here, [ForeignKey("Id")] it is not the PK of the MMTABS table, as it is PK of the MMMODL table trying to create one to many relationship with MMMODL and MMTABS

    hence I have written the above

  • User Avatar
    0
    aaron created
    Support Team

    Id is the PK of MMTABS table.

  • User Avatar
    0
    kalidarscope created

    yes, MMTABS have a default column named Id as PK.

    though Id is also the PK OF MMODL table.

    hence, How do I make relationship between MMODL and MMTABS.

  • User Avatar
    0
    aaron created
    Support Team

    You don't need ForeignKey attribute, it will be configured by convention: https://docs.microsoft.com/en-us/ef/core/modeling/relationships#conventions

  • User Avatar
    0
    kalidarscope created

    Thanks , going through the doc and let you know.

  • User Avatar
    0
    kalidarscope created

    You don't need ForeignKey attribute, it will be configured by convention: https://docs.microsoft.com/en-us/ef/core/modeling/relationships#conventions

    In this scenario both tables MMTABS & MMODL has same coulmn named Id as PK. so, how could make a FK relationship with convention method.

    [Table("MMTABS")] public class ASAPTabsEntity : FullAuditedEntity {

    public virtual int Id{get;set;}

    public virtual ASAPModulesEntity ASAPModulesEntities { get; set; } public virtual int Id { get; set; } }

    [Table("MMMODL")] public class ASAPModulesEntity : FullAuditedEntity { public virtual int Id{get;set;}

       public virtual ICollection&lt;ASAPTabsEntity&gt; ASAPTabsEntities { get; set; }
    

    }

    even It throws as , To change the IDENTITY property of a column, the column needs to be dropped and recreated.

  • User Avatar
    0
    aaron created
    Support Team

    Why are you defining additional Id properties?

    As I have mentioned, you don't need ForeignKey attribute, so just remove it from your original code and it will be configured by convention.

  • User Avatar
    0
    kalidarscope created

    please share sample code

  • User Avatar
    0
    aaron created
    Support Team
    [Table("MMTABS")]
    public class ASAPTabsEntity : FullAuditedEntity
    {
        // [ForeignKey("ASAPModulesEntitiesId")]
        public virtual ASAPModulesEntity ASAPModulesEntities { get; set; }
        public virtual int? PARENT_MODULE_ID { get; set; }
    }
    
    [Table("MMMODL")]
    public class ASAPModulesEntity : FullAuditedEntity
    {
        public virtual ICollection<ASAPTabsEntity> ASAPTabsEntities { get; set; }
    }
    
  • User Avatar
    0
    kalidarscope created

    Thanks. Its working.