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

前端 未结 2 529
猫巷女王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;}
    
    0 讨论(0)
  • 2020-12-23 12:40

    If you don't use the same context instance to load related entities you cannot simply add them to the new entity and expect that existing records in the database will be used. The new context doesn't know that these instances exist in the database - you must to say it to the context.

    Solutions:

    1. Use the same context for both loading related entities and saving new entity
    2. Don't load related entities and use dummy objects
    3. Load entities from the first context and detach them, attach entities to a new context and after that assign them to a new entity.
    4. After adding a new entity with relations manually change state of relations from Added to Unchanged

    Example for 1:

    var module = context.Modules.SingleOrDefault(m => m.ID == 1);
    var taskType = context.TaskTypes.SingleOrDefault(t => t.ID == 1);
    
    Task task = new Task();
    task.Module = module;
    task.Type = taskType;
    
    context.Tasks.Add(task);
    

    Example for 2:

    var module = new Module { Id = 1 };
    var taskType = new TaskType { Id = 1 };
    
    context.Modules.Attach(module);
    context.TaskTypes.Attach(taskType);
    
    Task task = new Task();
    task.Module = module;
    task.Type = taskType;
    
    context.Tasks.Add(task);
    

    Example for 3:

    var module = otherContext.Modules.SingleOrDefault(m => m.ID == 1);
    otherContext.Entry(module).State = EntityState.Detached;
    
    var taskType = otherContext.TaskTypes.SingleOrDefault(t => t.ID == 1);
    otherContext.Entry(taskType).State = EntityState.Detached;
    
    context.Modules.Attach(module);
    context.TaskTypes.Attach(taskType);
    
    Task task = new Task();
    task.Module = module;
    task.Type = taskType;
    
    context.Tasks.Add(task);
    

    Example for 4:

    var module = otherContext.Modules.SingleOrDefault(m => m.ID == 1);
    otherContext.Entry(module).State = EntityState.Detached;
    
    var taskType = otherContext.TaskTypes.SingleOrDefault(t => t.ID == 1);
    otherContext.Entry(taskType).State = EntityState.Detached;
    
    Task task = new Task();
    task.Module = module;
    task.Type = taskType;
    
    context.Tasks.Add(task);
    
    context.Entry(module).State = EntityState.Unchanged;
    context.Entry(taskType).State = EntityState.Unchanged;
    
    0 讨论(0)
提交回复
热议问题