Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:35:17+00:00 2026-06-10T20:35:17+00:00

Here is my base : And that it contains one movie (IDs comes from

  • 0

Here is my base :

And that it contains one movie (IDs comes from an external REST API) :

Movie movie = Movie.CreateMovie(999, "MyFirstMovie");
movie.Kinds.Add(Kind.Create(123,"Adventure"));
movie.Kinds.Add(Kind.Create(124,"Comic"));
movie.Actors.Add(Person.Create(321,"John Wayne"));
movie.Directors.Add(Person.Create(120,"John Woo"));
_context.AddToMovies(movie);
_context.SaveChanges();

Now, when I’m trying to insert a new movie, I got often an exception that said that I’m inserting an entity that already exists in the base.

Suppose I got another “Adventure” movie :

// Here all data comes from an external source and have no control over it.
using(Stream stream = myExternalStream)
{
   Movie movie = Unserialize(stream);
   _context.AddToMovies(movie);
}
// throws the exception because the kind "Adventure" already exists
_context.SaveChanges();

How can I avoid this exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:35:18+00:00Added an answer on June 10, 2026 at 8:35 pm

    I think you will have the check if the Kind already exists in the DB. If yes, replace the kind in the movie.Kinds collection by the kind loaded from the database. If no, keep the deserialized kind and create a new Kind entity, for example like so:

    using(Stream stream = myExternalStream)
    {
        Movie movie = Unserialize(stream);
        var newKindList = new List<Kind>();
        foreach(var kind in movie.Kinds)
        {
            var kindInDB = _context.Kinds
                .SingleOrDefault(k => k.KindId == kind.KindId);
            if (kindInDB != null)
                newKindList.Add(kindInDB);
            else
                newKindList.Add(kind);
        }
        movie.Kinds = newKindList;
        _context.AddToMovies(movie);
    }
    _context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need a Function Pointer from a base class. Here is the code: class
I have added one label in form that is not visible to user.Base on
I'm working on an application for one of our departments that contains medical data.
Complete prolog beginner here. Let's say I have a prolog knowledge base which contains
One of my base repository classes contains a method: public abstract class RepositoryBase<T, TDb>
I got 4 activites that all include a xml-footer which contains 4 buttons (one
I have several classes that are derived from SPPersistedObject. One of them is my
I need to return data from two tables - one table contains records on
Here is my code: class MyTestCase(Base): def setUp(self): #some code here def test_B(self): #some
Here what I am trying to do: BaseClass base = (BaseClass) AppDomain.CurrentDomain.CreateInstance(DerivedClassDLL,DerivedClass); However, there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.