locked
Foreign key return null while it's not RRS feed

  • Question

  • User1968449888 posted

    hey there

    I have two tables branches and cities.

    after running my code "City" returns null while it's not.

        public class Branch
        {
            public int BranchID { get; set; }
          
            [MaxLength(50)]
            public string BranchName { get; set; }
    
          
            [MaxLength(5)]
            public string BranchCode { get; set; }
            
            public int BranchStatus { get; set; }
            public City City { get; set; }
    
           
        }
    
        public class City 
        {
            public int CityID { get; set; }
            
            [MaxLength(50)]
            public string CityName { get; set; }
    
            public int CityCode { get; set; }
    
            public IEnumerable<Branch> Branches { get; set; }
    
        }
    var User = _db.Branches.Where(u => u.BranchID ==BranchCode).First();
    
    //User.City is null

    Tuesday, October 15, 2019 8:12 AM

Answers

  • User665608656 posted

    Hi Ocelot013,

    According to your code , I found that your two tables are connected by foreign keys, but in your model, the Branch table is not connected to the foreign key field of the City table, which is why User.City returns null.

    If you are using a model created by EF, I suggest that you add a CityID field to the Branch table in the database and set it as a foreign key to connect to the City table.

    Then rebuild the EF model, and the following fields will be generated automatically in the Branch class:

        public class Branch
        {
            public int BranchID { get; set; }
          
            [MaxLength(50)]
            public string BranchName { get; set; }
    
          
            [MaxLength(5)]
            public string BranchCode { get; set; }
            
            public int BranchStatus { get; set; }
    
            //Foreign key for City
            public int CityID { get; set; }
    
            public City City { get; set; }
    
           
        }

    For more details, you could refer to this link :

    Data Annotations - ForeignKey Attribute in EF 6 & EF Core

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 1:50 AM
  • User-1038772411 posted

    hello,

    You need to maintain relationship for getting data from child table.(Primarykey-Foreignkey)

    You can check below link for the entity framework relationship.

    Link : https://www.tutorialspoint.com/entity_framework/entity_framework_relationships.htm

    and then add Foreign key for "City" as follows

    public class Branch
    {
    public int BranchID { get; set; }
    
    [MaxLength(50)]
    public string BranchName { get; set; }
    
    [MaxLength(5)]
    public string BranchCode { get; set; }
    
    public int BranchStatus { get; set; }
    [ForeignKey("CityID")]
    public int CityID { get; set; }
    
    
    public City City { get; set; }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 4:58 AM

All replies

  • User475983607 posted

    You are missing the CityId in the Branch class.  See the EF relationship docs; https://docs.microsoft.com/en-us/ef/ef6/fundamentals/relationships

    Tuesday, October 15, 2019 11:18 AM
  • User665608656 posted

    Hi Ocelot013,

    According to your code , I found that your two tables are connected by foreign keys, but in your model, the Branch table is not connected to the foreign key field of the City table, which is why User.City returns null.

    If you are using a model created by EF, I suggest that you add a CityID field to the Branch table in the database and set it as a foreign key to connect to the City table.

    Then rebuild the EF model, and the following fields will be generated automatically in the Branch class:

        public class Branch
        {
            public int BranchID { get; set; }
          
            [MaxLength(50)]
            public string BranchName { get; set; }
    
          
            [MaxLength(5)]
            public string BranchCode { get; set; }
            
            public int BranchStatus { get; set; }
    
            //Foreign key for City
            public int CityID { get; set; }
    
            public City City { get; set; }
    
           
        }

    For more details, you could refer to this link :

    Data Annotations - ForeignKey Attribute in EF 6 & EF Core

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 1:50 AM
  • User-1038772411 posted

    hello,

    You need to maintain relationship for getting data from child table.(Primarykey-Foreignkey)

    You can check below link for the entity framework relationship.

    Link : https://www.tutorialspoint.com/entity_framework/entity_framework_relationships.htm

    and then add Foreign key for "City" as follows

    public class Branch
    {
    public int BranchID { get; set; }
    
    [MaxLength(50)]
    public string BranchName { get; set; }
    
    [MaxLength(5)]
    public string BranchCode { get; set; }
    
    public int BranchStatus { get; set; }
    [ForeignKey("CityID")]
    public int CityID { get; set; }
    
    
    public City City { get; set; }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 4:58 AM