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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mikkel
  • 1,771
  • 11
  • 35
  • 59
  • 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. – Gert Arnold Oct 02 '16 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. – Mikkel Oct 02 '16 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? – Gert Arnold Oct 02 '16 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.. – Mikkel Oct 02 '16 at 21:10
  • http://stackoverflow.com/questions/18709297/angularjs-ng-click-fires-twice – Gert Arnold Oct 02 '16 at 21:12
  • Thanks, didn't know it could be the problem,, will look into that.. I do have a click method in my angular2 app. will report back. – Mikkel Oct 02 '16 at 21:17

2 Answers2

1

Just use:

    context.RMovies.Add(movie);
    context.SaveChanges();
ErikEJ
  • 40,951
  • 5
  • 75
  • 115
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.

Community
  • 1
  • 1
Mikkel
  • 1,771
  • 11
  • 35
  • 59