【问题标题】:Entity Framework - Setting a "Nullable" integer to null实体框架 - 将“可空”整数设置为空
【发布时间】:2015-08-27 06:34:04
【问题描述】:

我的数据库中有一个名为Login 的表。在这个表中,我有一个名为Head_ID 的属性,它可以为空。

基本上,你可以有一个首领,或者你就是首领。如果您是主管,Head_ID 应该为空。在我的申请中,我有可能更换一个人的主管(同事更换主管,主管被降级并获得高于他的主管),但是这个int 不会设置为

Service.cs

public int EditLogin(LoginDTO login)
    {
        try
        {
            var dbLogin = DAO.HourRegInstance.Login.Single(x => x.ID == login.Id);
            dbLogin.Name = login.Name;
            dbLogin.Username = login.Username;
            if (login.Head_Id == 0)
            {
                //Doesn't work
                dbLogin.Head_ID = null;
            }
            dbLogin.Role_ID = login.Role_Id;
            DAO.HourRegInstance.SaveChanges();
            return 1;
        } catch(Exception e){
            return -1;
        }
    }

Login.cs

public partial class Login
{
    public Login()
    {
        this.HourRegistrationConfirmed = new HashSet<HourRegistrationConfirmed>();
        this.HourRegistrationConfirmed1 = new HashSet<HourRegistrationConfirmed>();
        this.Login1 = new HashSet<Login>();
        this.LoginProject = new HashSet<LoginProject>();
    }

    public long ID { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public long Role_ID { get; set; }
    public Nullable<long> Head_ID { get; set; }

    public virtual ICollection<HourRegistrationConfirmed> HourRegistrationConfirmed { get; set; }
    public virtual ICollection<HourRegistrationConfirmed> HourRegistrationConfirmed1 { get; set; }
    public virtual ICollection<Login> Login1 { get; set; }
    public virtual Login Login2 { get; set; }
    public virtual Role Role { get; set; }
    public virtual ICollection<LoginProject> LoginProject { get; set; }
}

如何完成这样的任务?

【问题讨论】:

标签: c# entity-framework wcf


【解决方案1】:

我自己解决了问题。

我不确定是什么导致了这个问题,但是在重新启动服务后,现在一切似乎都正常了。也许只是实体框架就是实体框架?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2010-09-13
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多