Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a foreign key to null when using entity framework code first

I'm using the database first implementation of Entity Framework Code First as the data layer for a project, but I've run into a problem.

I need to be able to set a foreign key to null in order to remove an association in the database.

I have 2 objects. One is called Project.

public class Project {     public int ProjectId {get; set;}     public Employee Employee {get;set;} }  public class Employee {     public int EmployeeId {get; set;}     public string EmployeeName {get;set;} } 

This matches what I have in the Database:

CREATE TABLE Project(     ProjectId int IDENTITY(1,1) NOT NULL,     EmployeeId int NULL )  CREATE TABLE Project(     EmployeeId int IDENTITY(1,1) NOT NULL,     EmployeeName varchar(100) NULL ) 

I can assign an Employee to a project. However, I want to be able to remove an employee from a project and have the Employee field be null. In my UI this will show as 'No EMployee Assigned'.

However, short of a direct sql query, I cannot seem to find a way to do this in the entity framework 4.1.

I've tried:

public void RemoveEmployeeFromProject(int projectId) {     var project = Context.Projects.FirstOrDefault(x => x.ProjectId == projectId);     project.Employee = null;     Context.SaveChanges(); } 

But this doesn't do anything.

Does anyone have any ideas?

like image 692
mccow002 Avatar asked Jun 21 '11 05:06

mccow002


People also ask

How do you make a foreign key nullable in code first approach?

A foreign key is NULLABLE by DEFAULT in code first asp.net mvc - 5 entity framework. If we want to make it non nullable. we need to either use fluent api if not then decorate with "Required" attribute.

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.

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 I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…


1 Answers

I think the problem is that as far as the context is concerned, you haven't actually changed anything.

You can use the lazy loading approach previously suggested by using virtual, but since you haven't requested that the Employee be loaded yet, it's still null. You could try this:

var forceLoad = project.Employee; project.Employee = null; // Now EF knows something has changed Context.SaveChanges(); 

Alternatively, explicitly include it in your original request:

var project = Context.Projects.Include(x => x.Employee).FirstOrDefault(x => x.ProjectId == projectId); project.Employee = null; Context.SaveChanges(); 

On a side note, FirstOrDefault will return null if no Project matches the given id. If you know the project exists, you can just use First. You could even use Single which will assert that there is only one such project. If you continue to use FirstOrDefault, I'd recommend checking for null before working with project.

like image 112
David Ruttka Avatar answered Sep 19 '22 03:09

David Ruttka