1

I could really need some help in order to solve this issue. When I try to add an entity using Entity Framework, it keeps adding 1 more than needed.

Here you see my database after I have added 2 movies.

enter image description here

As you see, it adds the same movie "The rock" twice.

Been looking into the problem the past two days, but haven't found a solution that don't giving my exceptions.

Code:

public bool Execute(RequestedMovie movie)
    {
        using (var context = new MoviesContext())
        {
            context.RMovies.Attach(movie);
            context.RMovies.Add(movie);
            context.SaveChanges();
        }

        return true;

    }

Model:

public class RequestedMovie
{
    [Key]
    public int RequestedMoviesID { get; set; }
    public string MovieId { get; set; }
    public string MovieTitle { get; set; }
    public string MovieLink { get; set; }
    public string MovieYear { get; set; }
    public int MovieQuality { get; set; }
    public string Requester { get; set; }
    public bool Status { get; set; }

}

DataContext:

public class MoviesContext : DbContext, IMoviesContext
{
    public MoviesContext() : base("MoviesContext")
    {

    }

    // DbSet to bookings
    public DbSet<Movie> Movies { get; set; }
    public DbSet<RequestedMovie> RMovies { get; set; }

    public void MarkAsAdded(Movie item)
    {
        Entry(item).State = EntityState.Added;
    }

    public void MarkAsDeleted(Movie item)
    {
        Entry(item).State = EntityState.Deleted;
    }

    public void MarkRequestedMovieAsAdded(RequestedMovie item)
    {
        Entry(item).State = EntityState.Added;
    }

    public void MarkRequestedMovieAsModified(RequestedMovie item)
    {
        Entry(item).State = EntityState.Modified;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    }
}

This should be pretty strait forward, because I only have one table which I'm going to add to. Have tried with the Attach approach that I found in another Stack post, but it still won't work :(.

Have also tried using the methods (MarkRequestedMovieAsAdded) I have in my context file, instead of RMovies.Add(objekt), but same result.

What could be wrong here?

6
  • Well, you do context.RMovies.Add(movie); (the preceding attach is irrelevant). So if you enter the same movie again as RequestedMovie it will be inserted. EF only does the job you ask it to do. Commented Oct 2, 2016 at 18:49
  • I Think it has something to do with the context, but I'm not sure at all.. still looking into this! it only runs one time as I see it, but inserts two rows. Commented Oct 2, 2016 at 20:27
  • OK, so it seems to run once but it inserts twice.What kind of application is this? Which process executes the Execute method? Commented Oct 2, 2016 at 21:07
  • Angular2 application calling this Web Api which should post a movie.. Right now I'm trying to remove the second object right after Entity framework have inserted it.. Commented Oct 2, 2016 at 21:10
  • stackoverflow.com/questions/18709297/… Commented Oct 2, 2016 at 21:12

2 Answers 2

1

Just use:

    context.RMovies.Add(movie);
    context.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

0

I managed to solve this issue. I haven't done any mistake in the Web Api. It turned out that my Angular2 observable calls made an error and called my web api twice because it was (cold) and not (warm).

Here is the post about it: Angular2 http.post gets executed twice

All I should do was add .share() after mapping in my angular2 service.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.