0

I have a one-to-many relationship in my domain similar to the following:

public class Movie
{
    public int MovieID { get; set; }
    public string MovieName { get; set; }      

    public virtual ICollection<MovieCategory> Categories { get; set; }
}

public class MovieCategory
{
    [Key, ForeignKey("Movie")]
    public int MovieID { get; set; }

    [Key, ForeignKey("Category")]
    public int CategoryID { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public DateTime DateCreated { get; set; }

    public virtual Movie Movie { get; set; }
    public virtual Category Category { get; set; }
}

The DateCreated field is being set in SQL via GETDATE() upon inserting new movie (and checking off some categories in a bunch of checkboxes). That part works fine.

However, when editing a movie, I need to be able to update the selection of movie categories (which means adding some and/or removing others, based on what the user has done with the selection in checkboxes). I tried this:

public ActionResult Edit(MovieEditViewModel model)
{
    var movie = db.Movies.Find(model.MovieID);

    //clear out all previously selected categories
    movie.Categories.Clear();

    //add the list of selected categories from just-edited movie
    foreach(var catID in model.selectedCategories)
    {
        movie.Categories.Add(new MovieCategory { CategoryID = catID });
    }

    db.Entry(movie).State = EntityState.Modified;
    db.SaveChanges();
}

I guess EF is smart enough to figure out which categories need to be deleted, which ones need to be added, and which ones need to be modified.

The only problem is, for the ones it's modifying, it tries to modify every field, including the DateCreated field, and since that one is only supposed to be touched during insert (i.e. 'Identity'), it causes this error:

"Modifying a column with the 'Identity' pattern is not supported. Column: 'DateCreated'. Table: 'CodeFirstDatabaseSchema.MovieCategory'."

Any ideas how I can get past this? All I'm trying to do is allow somebody to edit a movie, where one may or may not modify the list of movie categories during the edit, and the table MovieCategory in DB needs to reflect the edited selection accurately.

I also tried adding this, but no change:

foreach(var category in movie.Categories)
{
    db.Entry(category).Property(c => c.DateCreated).IsModified = false;
}

1 Answer 1

5

DatabaseGeneratedOption.Identity is for identity columns. These are auto generated as well, but they're never intended to be modified because these columns are commonly used as primary keys.

You should change the option to

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateCreated { get; set; }

When you do this, EF will ignore the property in insert and update statements and read its value from the database right after these statements.

If you do this, your code should run fine.

Side note, the statement

db.Entry(movie).State = EntityState.Modified;

is redundant, because it only affects the movie's scalar properties, not its Categories. And movie itself is not modified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.