title: Creating inheritance users from base asp.net identity user
body: <p>I have problem in which i would like to create N, two in the example, user objects (e.g. Customer &amp; Supplier) which all inherent from the asp.net IdentityUser object. These object have very different additional data besides the the data from the IdentityUser. I would like to use the IdentityUser user as this gives me a flexible way of taking care of authentication and authorization.</p>
  
  <p>This example has been very stripped down but should supply sufficient information concerning the not being able to create a concrete user (e.g. Customer of Supplier). It seems i need to use the UserManager object as this also takes care of creating for example the password hash and additional security information.</p>
  
  <p>I get presented the following error:</p>
  
  <p><em>{"Attaching an entity of type 'Supplier' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate."}</em></p>
  
  <p><strong>Classes which inherent from IdentityUser</strong></p>
  
  <pre><code> public class Customer : IdentityUser
   {
      public string CustomerProperty { get; set; }
   }
  
   public class Supplier : IdentityUser
   {
      public string SupplierProperty { get; set; }
   }
  </code></pre>
  
  <p><strong>Database context class</strong></p>
  
  <pre><code> public class ApplicationDbContext : IdentityDbContext {
  
        public ApplicationDbContext() : base("ApplicationDbContext")
        {
           Database.SetInitializer(new ApplicationDbInitializer());
        }
  
        public DbSet&lt;Customer&gt; CustomerCollection { get; set; }
        public DbSet&lt;Supplier&gt; SupplierCollection { get; set; }
   }
  </code></pre>
  
  <p><strong>Seeding class which throws the exception</strong></p>
  
  <pre><code> public class ApplicationDbInitializer : DropCreateDatabaseAlways&lt;ApplicationDbContext&gt;
   {
      protected override void Seed(ApplicationDbContext context)
      {
          var userStore = new UserStore(context);
          var userManager = new UserManager(userStore);
  
  
          // Seed customer user which inherents from asp.net IdentityUser 
          var user = userManager.FindByEmail("customer@customer.com");
          if (user == null)
          {
              user = new User()
              {
                  UserName = "customer@customer.com",
                  Email = "customer@customer.com"
              };
  
              userManager.Create(user, userPassword);
  
              var customerUser = new Customer()
              {
                  Id = user.Id,
                  CustomerProperty = "Additional Info"
              };
  
              context.Entry(customerUser).State = EntityState.Modified;
              context.SaveChanges();
          }
  
          // Seed supplier user which inherents from asp.net IdentityUser 
          var user = userManager.FindByEmail("supplier@supplier.com");
          if (user == null)
          {
              user = new User()
              {
                  UserName = "supplier@supplier.com",
                  Email = "supplier@supplier.com"
              };
  
              userManager.Create(user, userPassword);
  
              var supplierUser = new Supplier()
              {
                  Id = user.Id,
                  IBAN = "212323424342234",
                  Relationship = "OK"
              };
  
              context.Entry(supplierUser).State = EntityState.Modified;
              context.SaveChanges();
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>**** UPDATE ****</p>
  
  <p>The solution below works but i am still struggling with two issues:</p>
  
  <ol>
  <li>I would always like to have one user type (e.g. Customer of Supplier) associated with the IdentityUser. I though about using an interface but this doesn't work.</li>
  <li>If i also add the virtual reference towards the IdentityUser on the user types i get an 'Unable to determine the principal end of an association between the types 'ApplicaitonUser' and 'Supplier'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.' exception.</li>
  </ol>
  
  <p><strong>Classes</strong></p>
  
  <pre><code> public class Customer 
   {
      [Key]
      public int CustomerId { get;set; }
      public string CustomerProperty { get; set; }
  
      *public virtual User User { get; set; }*
  
   }
  
   public class Supplier 
   {
      [Key]
      public int SupplierId { get;set; }
      public string SupplierProperty { get; set; }
  
      *public virtual User User { get; set; }*
   }
  </code></pre>
  
  <p>**Class IdentityUser (which works) **</p>
  
  <pre><code>public class User : IdentityUser
  {
      public virtual Supplier Supplier { get; set; }
      public virtual Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>**Class IdentityUser (what i would like) **</p>
  
  <pre><code>public class User : IdentityUser
  {
      public virtual IConcreteUser ConcreteUser{ get; set; }
  }
  </code></pre>
  
  <p><strong>Database context class</strong></p>
  
  <pre><code> public class ApplicationDbContext : IdentityDbContext {
  
        public ApplicationDbContext() : base("ApplicationDbContext")
        {
           Database.SetInitializer(new ApplicationDbInitializer());
        }
  
        public DbSet&lt;Customer&gt; CustomerCollection { get; set; }
        public DbSet&lt;Supplier&gt; SupplierCollection { get; set; }
   }
  </code></pre>
  
  <p>**Seeding class **</p>
  
  <pre><code> public class ApplicationDbInitializer : DropCreateDatabaseAlways&lt;ApplicationDbContext&gt;
   {
  protected override void Seed(ApplicationDbContext context)
  {
      var userStore = new UserStore(context);
      var userManager = new UserManager(userStore);
      var roleManager = new RoleManager(roleStore);
  
      var user = userManager.FindByEmail("customer@customer.com");
      if (user == null)
      {
          user = new ApplicationUser()
          {
              UserName = "customer@customer.com",
              Email = "customer@customer.com"
              Customer = new Customer()
              {
                  CustomerProperty = "Additional Info"
              }
          };
  
          userManager.Create(user, userPassword);
          roleManager.AddUserToRole("Customer");
      }
  
      user = userManager.FindByEmail("supplier@supplier.com");
      if (user == null)
      {
          user = new ApplicationUser()
          {
              UserName = "supplier@supplier.com",
              Email = "supplier@supplier.com",
              Supplier = new Supplier()
              {
                  IBAN = "212323424342234",
                  Relationship = "OK"
              }
          };
  
          userManager.Create(user, userPassword);
          roleManager.AddUserToRole("Supplier");
      }
  }
  </code></pre>
  
  <p>}</p>