实体框架设置导航属性设置为null [英] Entity Framework set navigation property to null

查看:173
本文介绍了实体框架设置导航属性设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体框架数据库的第一个项目。这里是模型的提取:



<预类=郎-CS prettyprint-覆盖> 公共部分类LedProject
{
公共LedProject()
{
this.References =新的HashSet< LedProjectReference>();
this.Results =新的HashSet< LedProjectResult>();
this.History =新的HashSet< LedProjectHistory>();
}

公共字符串标识{搞定;组; }
公共字符串名称{;组; }
公众可空<&System.DateTime的GT; CompletionDate {搞定;组; }
公众的System.DateTime CreationDate {搞定;组; }
公众的System.Guid专案编号{搞定;组; }
公共字符串评论{搞定;组; }

公共虚拟用户ContactUser {搞定;组; }
公共虚拟用户CreationUser {搞定;组; }
公共虚拟客户客户{搞定;组; }
公共虚拟LedProjectAccounting会计{搞定;组; }
公共虚拟LedProjectState国家{搞定;组; }
公共虚拟的ICollection< LedProjectReference>参考{搞定;组; }
公共虚拟的ICollection< LedProjectResult>结果{搞定;组; }
公共虚拟用户ResponsibleUser {搞定;组; }
公共虚拟的ICollection< LedProjectHistory>历史{搞定;组; }
}



<预类=郎-CS prettyprint-覆盖> 公共部分类用户
{
公众的System.Guid用户ID {搞定;组; }
公共字符串的LoginName {搞定;组; }
公众的System.DateTime CreationDate {搞定;组; }
公共字符串名字{获得;组; }
公共字符串姓氏{搞定;组; }
公共字符串电子邮件{获得;组; }
}



我有设置导航项目问题ResponsibleUser 类的 LedProject 。当我设置了 ResponsibleUser 到另一个用户,之后保存的DbContext的改变,这些改变都存储在数据库中。



但是,当我想 ResponsibleUser LedProject ,由导航属性设置为删除当前空值。这些变化并不存储在数据库中。



<预类=郎-CS prettyprint-覆盖> LedProject项目= db.LedProject.Find(专案编号);
project.Name = string.IsNullOrEmpty(名称)?空:名称;
...
project.ResponsibleUser = responsibleUser == NULL?空:db.User.Find(responsibleUser.UserId);
...
db.SaveChanges();



有没有删除导航属性的把戏?


解决方案

问题在于导航属性的延迟加载。看来,值首先设置为空,之后从数据库加载。因此,所需的值(在我的情况)是由数据库中当前存储的值覆盖。



<预类=郎-CS prettyprint-覆盖> LedProject项目= db.LedProject
.INCLUDE(ResponsibleUser)
。凡(p => p.ProjectId ==专案编号)
.FirstOrDefault ();

这加载在 ResponsibleUser 项目被加载。这终于解决了我的问题!


I have a entity framework database first project. here is a extraction of the model:

public partial class LedProject
{
    public LedProject()
    {
        this.References = new HashSet<LedProjectReference>();
        this.Results = new HashSet<LedProjectResult>();
        this.History = new HashSet<LedProjectHistory>();
    }

    public string Identifier { get; set; }
    public string Name { get; set; }
    public Nullable<System.DateTime> CompletionDate { get; set; }
    public System.DateTime CreationDate { get; set; }
    public System.Guid ProjectId { get; set; }
    public string Comment { get; set; }

    public virtual User ContactUser { get; set; }
    public virtual User CreationUser { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual LedProjectAccounting Accounting { get; set; }
    public virtual LedProjectState State { get; set; }
    public virtual ICollection<LedProjectReference> References { get; set; }
    public virtual ICollection<LedProjectResult> Results { get; set; }
    public virtual User ResponsibleUser { get; set; }
    public virtual ICollection<LedProjectHistory> History { get; set; }
}

public partial class User
{
    public System.Guid UserId { get; set; }
    public string LoginName { get; set; }
    public System.DateTime CreationDate { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
}

I have a problem with setting the navigation item ResponsibleUser of the class LedProject. When I set the ResponsibleUser to a another user and afterwards save the changes of the DBContext, the changes are stored in the database.

But, when I want to delete the current ResponsibleUser of an LedProject, by setting the navigation property to null. The changes are not stored in the database.

LedProject project = db.LedProject.Find(projectId);
project.Name = string.IsNullOrEmpty(name) ? null : name;
...
project.ResponsibleUser = responsibleUser == null ? null : db.User.Find(responsibleUser.UserId);
...
db.SaveChanges();

Is there any trick for deleting navigation properties?

解决方案

The problem lies in the lazy loading of the navigation property. It seems that the value is first set to null and afterwards loaded from the database. So the desired value (null in my case) is overridden by the currently stored value in the database.

LedProject project = db.LedProject
    .Include("ResponsibleUser")
    .Where(p => p.ProjectId == projectId)
    .FirstOrDefault();

This loads the ResponsibleUser when the Project is loaded. This finally solved my issue!

这篇关于实体框架设置导航属性设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆