简体   繁体   中英

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.

First of all let me clarify the scenario: I'm building a website using ASP.NET-webforms, code behind is written in C#. As ORM tool I implemented EF6 with a database-first approach. 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. The corresponding class to the table in EF6 looks like this:

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.

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. 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. On database side this column is of the datatype "bit" with default "0" or "false", also it's set to not nullable.

So when the DeleteMethod is called I basically want to update the entity that the user is working on.

Problem is that I'm unable to perform that update on the Deleted column properly.

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". Otherway around (setting a project that is marked as "Deleted = true" to "Deleted = false") also doesn't work.

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. 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.

So I found the Problem. @Gert Arnold kinda helped me finding it. In my EDMX file of the database model the StoreGeneratedPattern property of the Deleted column was set to "Computed". I changed it to "None", which made it work as intended.

Thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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