Linq to SQL Generic Repository Pattern Update Issue
<p>I have a weird problem. I am using Generic Repository pattern and Linq to SQL. I have many to many relations between objects which are dbml generated objects and here is the problem. I am getting an error when I try to update the <strong>Player</strong> object. The error occures when I try to update <strong>name of player's skill.</strong> This is the exception I'm getting:</p>

<blockquote>
  <blockquote>
    <p>An exception of type 'System.InvalidOperationException' occurred in System.Data.Linq.dll but was not handled in user code</p>
    
    <p>Additional information: An attempt was made to remove a relationship between a Skill and a PlayerSkill. However, one of the relationship's foreign keys (PlayerSkill.Skill_ID) cannot be set to null.</p>
  </blockquote>
</blockquote>

<p>Here is the <strong>Update</strong> method.</p>

<pre><code> public void Update(PlayerEntity player)
    {
        Player p = Mapper.Map&lt;PlayerEntity, Player&gt;(player);
        _unitOfWork.SkillRepository.AddOrUpdate(p.PlayerSkills.Select(i =&gt; i.Skill).ToList());
        _unitOfWork.ProfileRepository.AddOrUpdate(p.Profile);
        _unitOfWork.SocialAccountRepository.AddOrUpdate(p.SocialAccount);
        _unitOfWork.PlayerRepository.AddOrUpdate(p);
    }
</code></pre>

<p><strong>AddOrUpdate</strong> method on repo:</p>

<pre><code>public void AddOrUpdate(ICollection&lt;TEntity&gt; entities)
    {
        foreach (var e in entities)
        {
            AddOrUpdate(e);
        }
    }

public void AddOrUpdate(TEntity entity)
    {
        if (GetPrimaryKeyValue(entity) &gt; 0)
        {
            Update(entity);
        }
        else
        {
            Insert(entity);
        }
    }
</code></pre>

<p><strong>Update</strong> method on DataLINQ layer</p>

<pre><code>public void Update(TEntity entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        var original = GetById(GetPrimaryKeyValue(entity));

        ApplyChanges(original, entity);

        _context.SubmitChanges();
    }
</code></pre>

<p>Lastly; <strong>ApplyChanges</strong></p>

<pre><code>private void ApplyChanges&lt;F, S&gt;(F originalEntity, S newEntity)
    {
        var entityType = typeof(F);
        var entityProperties = entityType.GetProperties();
        foreach (var propertyInfo in entityProperties)
        {
            var currentProperty = entityType.GetProperty(propertyInfo.Name);
            currentProperty.SetValue(originalEntity, propertyInfo.GetValue(newEntity, null));
        }
    }
</code></pre>

<p>I call the object as follows: </p>

<pre><code>public IHttpActionResult PutPlayer(int id, PlayerEntity player)
    {
        if (player == null)
        {
            return NotFound();
        }
        _playerService.Update(player);

        return Ok();
    }
</code></pre>

<p><strong>Note:</strong> I use AutoMapper to map the objects, but I don't think that is related with error. Thanks in adv.</p>