Entity Framework creates foreign key objects instead of using those that are already available

前端 未结 2 533
猫巷女王i
猫巷女王i 2020-12-23 11:43

my current project is based on Entity Framwork code-first. I have three types: Task, TaskType and Module.

    public class Task
    {
        public int ID {         


        
2条回答
  •  星月不相逢
    2020-12-23 12:28

    I'm new to entity framework but I just ran into the same issue, where my foreign key property was populated but the navigation object was null, with code like:

    public class EntryRecord
    {        
        [Key]
        public int Id { get; set; }
    }
    public class QueueItem 
    {       
        [Key]
        public int Id { get; set; }
    
    
        public int EntryRecordId { get; set; }
    
        [ForeignKey("EntryRecordId")]
        public EntryRecord EntryRecord { get; set;}
    

    }

    Setting the navigation property to virtual fixes the problem and enables lazy loading :

        [ForeignKey("EntryRecordId")]
        public virtual EntryRecord EntryRecord { get; set;}
    

提交回复
热议问题