第一次使用实体框架的代码时设置的外键空 [英] Setting a foreign key to null when using entity framework code first
问题描述
我使用该数据库第一个实现实体框架代码首先,作为一个项目的数据层,但我碰到的一个问题。
我需要能够设置一个外键空为了除去在数据库中关联。
我有2对象。其中一个被称为项目
公共类项目
{
公众诠释专案编号{搞定;设置;}
公共雇员雇员{获取;设置;}
}
公共类员工
{
公众诠释雇员{搞定;设置;}
公共字符串EmployeeName {获取;设置;}
}
这与我有什么在数据库:
<预类=郎-SQL prettyprint-覆盖>
CREATE TABLE项目(
专案编号INT IDENTITY(1,1)NOT NULL,
雇员INT NULL
)
CREATE TABLE项目(
雇员INT IDENTITY(1,1)NOT NULL,
EmployeeName为varchar(100)NULL
)
我可以将员工分配到项目。不过,我希望能够从项目中删除雇员,并有员工现场为空。在我的UI,这将显示为无内部职工分配。
不过,短期直接使用SQL查询,我似乎无法找到一种方法,在实体做到这一点框架4.1。
我试过:
公共无效RemoveEmployeeFromProject(INT专案编号)
{
VAR项目= Context.Projects.FirstOrDefault(X => x.ProjectId ==专案编号);
project.Employee = NULL;
Context.SaveChanges();
}
但是,这并不做任何事情。
有没有人有什么想法?
我认为问题是,尽可能的上下文关注,您还没有真正改变任何东西。
您可以使用以前使用建议延迟加载方法虚拟
,但因为你还没有要求该雇员被没有加载,它仍然是零。你可以试试这个:
VAR的forceload = project.Employee;
project.Employee = NULL; //现在EF知道事情已经改变
Context.SaveChanges();
另外,明确包括在您的原始请求:
VAR项目= Context.Projects.Include(X => x.Employee).FirstOrDefault(X => x.ProjectId ==专案编号);
project.Employee = NULL;
Context.SaveChanges();
在一个侧面说明, FirstOrDefault
将返回空
如果没有项目
定id匹配。如果你知道这个项目存在,你可以使用首先
。你甚至可以使用单
将断言,只有一个这样的项目。如果继续使用 FirstOrDefault
,我建议你用项目前检查
。空
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?
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
.
这篇关于第一次使用实体框架的代码时设置的外键空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!