Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

    public class Task     {         public int ID { get; set; }         public Module Module { get; set; }         public TaskType Type { get; set; }     }      public class TaskType     {         public int ID { get; set; }         public string Name { get; set; }     }      public class Module     {         public int ID { get; set; }         public string Name { get; set; }     } 

There are foreign key relations defined inside the table for the Task-type.

My problem is that when I try to create a new Task-object linked to already available TaskType and Module objects (ID = 1), those objects are created as new rows in their corresponding tables.

        TaskRepository repo = new TaskRepository();          Task task = new Task();         task.Module = Modules.SingleOrDefault(m => m.ID == 1);         task.Type = TaskTypes.SingleOrDefault(t => t.ID == 1);          Tasks.Add(task); 

This creates a new row in my TaskType-table and in my Modules-table as well instead of just using the already available TaskType-ID and Module-ID.

I hope I made clear what my problem is ;-)

Thanks in advance for your help. I appreciate it.

Regards, Kevin

like image 738
bitfrickler Avatar asked Jun 06 '11 09:06

bitfrickler


People also ask

Does Entity Framework support foreign keys?

When you change the relationship of the objects attached to the context by using one of the methods described above, Entity Framework needs to keep foreign keys, references, and collections in sync.

How do you set a foreign key in an entity?

The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.

How do I set a foreign key in EF core?

If you want the foreign key to reference a property other than the primary key, you can use the Fluent API to configure the principal key property for the relationship. The property that you configure as the principal key will automatically be set up as an alternate key.


1 Answers

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; 
like image 108
Ladislav Mrnka Avatar answered Oct 02 '22 05:10

Ladislav Mrnka