简体   繁体   English

实体框架 6 中特定列的更新不起作用

[英]Update on specific column in Entity Framework 6 not working

I got a problem updating a specific column in my database using Entity Framework 6.我在使用 Entity Framework 6 更新数据库中的特定列时遇到问题。

First of all let me clarify the scenario: I'm building a website using ASP.NET-webforms, code behind is written in C#.首先让我澄清一下场景:我正在使用 ASP.NET-webforms 构建一个网站,后面的代码是用 C# 编写的。 As ORM tool I implemented EF6 with a database-first approach.作为 ORM 工具,我使用数据库优先的方法实现了 EF6。 The website is supposed to help organizing projects.该网站应该帮助组织项目。

My most important table on the database is the project table, where the base data for each individual project is stored.我在数据库中最重要的表是project表,其中存储了每个单独项目的基本数据。 The corresponding class to the table in EF6 looks like this: EF6 中表的对应类如下所示:

public partial class Project
{
    public Project()
    {
        this.ProjectRessource = new HashSet<ProjectRessource>();
        this.ProjectTeam = new HashSet<ProjectTeam>();
    }

    public int ProjectID { get; set; }
    public string Name { get; set; }
    public Nullable<int> ManagerID { get; set; }
    public string Goal { get; set; }
    public Nullable<int> PhaseID { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> EndDate { get; set; }
    public Nullable<int> PriorityID { get; set; }
    public Nullable<decimal> Progress { get; set; }
    public string Comment { get; set; }
    public string Decisions { get; set; }
    public string File { get; set; }
    public Nullable<int> StatusID { get; set; }
    public Nullable<int> PlannedDays { get; set; }
    public Nullable<int> PlannedCost { get; set; }
    public Nullable<short> PlannedYear { get; set; }
    public bool Deleted { get; set; }

    public virtual Status Status { get; set; }
    public virtual Phase Phase { get; set; }
    public virtual Priority Priority { get; set; }
    public virtual Employee Employee { get; set; }
    public virtual ICollection<ProjectRessource> ProjectRessource { get; set; }
    public virtual ICollection<ProjectTeam> ProjectTeam { get; set; }
}

Obviously there are more tables but I don't think they matter regarding my problem.显然有更多的桌子,但我认为它们对我的问题并不重要。

On my website I got a formview to display the data for individual projects, selected by the user.在我的网站上,我有一个表单视图来显示用户选择的各个项目的数据。

To connect the formview and the requested data I'm using an ObjectDataSource, which is connected to the different query-methods in my DAL.为了连接表单视图和请求的数据,我使用了一个 ObjectDataSource,它连接到我的 DAL 中的不同查询方法。

When a user wants to delete a project there is a button in my formview with CommandName "Delete" which triggers the DeleteMethod of the ObjectDataSource connected to the formview.当用户想要删除一个项目时,我的表单视图中有一个带有 CommandName“Delete”的按钮,它会触发连接到表单视图的 ObjectDataSource 的 DeleteMethod。 So far everything works perfect and as intended.到目前为止,一切都按预期完美运行。 The thing is, that I don't really want to delete the record but i want to set the property "Deleted" (see in the Project-class above) to true.问题是,我真的不想删除记录,但我想将属性“已删除”(参见上面的项目类)设置为 true。 On database side this column is of the datatype "bit" with default "0" or "false", also it's set to not nullable.在数据库端,此列的数据类型为“bit”,默认为“0”或“false”,也设置为不可为空。

So when the DeleteMethod is called I basically want to update the entity that the user is working on.因此,当调用 DeleteMethod 时,我基本上想更新用户正在处理的实体。

Problem is that I'm unable to perform that update on the Deleted column properly.问题是我无法在Deleted列上正确执行该更新。

The method looks like this:该方法如下所示:

public void DeleteProject(Project project)
{
        project.Deleted = true;

        context.Project.Attach(project);
        context.Entry(project).State = EntityState.Modified;
        context.SaveChanges();
}

The "context" is my database context. “上下文”是我的数据库上下文。

After the method is called I'm checking the database but the "Deleted"-field is still set to "0".调用该方法后,我正在检查数据库,但“已删除”字段仍设置为“0”。 Otherway around (setting a project that is marked as "Deleted = true" to "Deleted = false") also doesn't work.否则(将标记为“Deleted = true”的项目设置为“Deleted = false”)也不起作用。

I already checked other properties.我已经检查了其他属性。 For example if I change code to the following:例如,如果我将代码更改为以下内容:

public void DeleteProject(Project project)
{
        Project pro1 = new Project
        {
            ProjectID = project.ProjectID,
            Name = "68",
            Deleted = false,
            Goal = "68",
            ManagerID = 8,
            PlannedCost = 68,
            PlannedYear = 2015,
            StartDate = DateTime.Now,
            EndDate = DateTime.Now,
            PhaseID = 1,
            PriorityID = 1,
            Progress = 0,
            Comment = "68",
            Decisions = "68",
            File = "68",
            PlannedDays = 68
        };

        context.Project.Attach(pro1);
        context.Entry(pro1).State = EntityState.Modified;
        context.SaveChanges();
}

Literally every other column in my database table is updated, but not the Deleted column, and to be honest I got no clue why that is.从字面上看,我的数据库表中的所有其他列都会更新,但不是Deleted列,老实说,我不知道为什么会这样。 I'm probably missing really basic stuff here but I can't seem to find it.我可能在这里遗漏了非常基本的东西,但我似乎找不到它。

I would really appreciate some help on that one.我真的很感激那方面的一些帮助。

In case you need more information about something in my code, just leave a comment below.如果您需要有关我代码中某些内容的更多信息,请在下面发表评论。

Thanks in advance for the help!在此先感谢您的帮助!

EDIT:编辑:

My database is a MS SQL Server 2016.我的数据库是 MS SQL Server 2016。

So I found the Problem.所以我发现了问题。 @Gert Arnold kinda helped me finding it. @Gert Arnold 帮助我找到了它。 In my EDMX file of the database model the StoreGeneratedPattern property of the Deleted column was set to "Computed".在我的数据库模型的 EDMX 文件中, Deleted列的StoreGeneratedPattern属性设置为“Computed”。 I changed it to "None", which made it work as intended.我将其更改为“无”,使其按预期工作。

Thanks for the help!谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM