title: Entity Framework: Many-to-Many relations with abstract classes, modify without querying database?
body: <p>With Entity Framework, if I have the following model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Asset {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;AssetGroup&gt; Groups { get; set; }
  }
  
  class AssetGroup {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;Asset&gt; Assets { get; set; }
  }
  </code></pre>
  
  <p>Adding an existing asset to a given group without having to query the database to load the asset in question is relatively straightforward:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using(var context = new MyContext()) {
      AssetGroup assetGroup = context.AssetGroups.Find(groupId);
  
      // Create a fake Asset to avoid a db query
      Asset temp = context.Assets.Create();
      temp.Id = assetId;
      context.Assets.Attach(temp);
  
      assetGroup.Assets.Add(temp);
      context.SaveChanges();
  }
  </code></pre>
  
  <p>However, I now have the problem that I have extended the model so that there are multiple Asset types, implemented as an inheritance hierarchy. In the process, the Asset class has been made abstract, since an asset with no specific type makes no sense:</p>
  
  <pre class="lang-cs prettyprint-override"><code>abstract class Asset {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;AssetGroup&gt; Groups { get; set; }
  }
  
  class SpecificAsset1 : Asset {
      public string OneProperty { get; set; }
  }
  
  class SpecificAsset2 : Asset {
      public string OtherProperty { get; set; }
  }
  </code></pre>
  
  <p>The problem is that now the <code>Create()</code> call throws an <code>InvalidOperationException</code> because "Instances of abstract classes cannot be created", which of course makes sense.</p>
  
  <p>So, the question is, how can I update the many-to-many relation without having to go fetch the Asset from the database?</p>