title: Batch Insert - Foreign Key Not Working
body: <p>I'm trying to do a batch insert and it's not working. I thought I had this working but something seems to have broken and I'd appreciate it if someone could show me what.</p>
  
  <p><strong>Edit</strong> - Here's the database schema:</p>
  
  <pre><code>CREATE TABLE [dbo].[Categories](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Name] [nvarchar](100) NOT NULL,
      CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED ([Id])
  )
  
  CREATE TABLE [dbo].[ProductTopSellersCategory](
      [ProductId] [int] NOT NULL,
      [CategoryId] [int] NOT NULL,
      [Order] [int] NOT NULL,
      CONSTRAINT [PK_ProductTopSellersCategory]
            PRIMARY KEY CLUSTERED ([ProductId], [CategoryId])
  )
  
  ALTER TABLE [dbo].[ProductTopSellersCategory] ADD
      CONSTRAINT [FK_ProductTopSellersCategory_Products]
          FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Products] ([Id]),
      CONSTRAINT [FK_ProductTopSellersCategory_Categories]
          FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Categories] ([Id])
  </code></pre>
  
  <p>I have the following entities:</p>
  
  <pre><code>public class Category {
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
  }
  
  public class ProductTopSellerCategory {
      public virtual ProductTopSellerCategoryIdentifier Id { get; set; }
  
      private Product _product;
      public virtual Product Product {
          get { return _product; }
          set { _product = value; Id.ProductId = _product.Id; }
      }
  
      private Category _category;
      public virtual Category Category {
          get { return _category; }
          set { _category = value; Id.CategoryId = _category.Id; }
      }
  
      [Required]
      public virtual int Order { get; set; }
  
      public ProductTopSellerCategory() {
          Id = new ProductTopSellerCategoryIdentifier();
      }
  }
  
  [Serializable]
  public class ProductTopSellerCategoryIdentifier {
      public virtual int ProductId { get; set; }
      public virtual int CategoryId { get; set; }
  
      #region Composite Id Members
  
      public override bool Equals(object obj) {
          if (obj == null || !(obj is ProductTopSellerCategoryIdentifier))
              return false;
  
          var i = (ProductTopSellerCategoryIdentifier)obj;
  
          return ProductId == i.ProductId &amp;&amp; CategoryId == i.CategoryId;
      }
  
      public override int GetHashCode() {
          return ToString().GetHashCode();
      }
  
      public override string ToString() {
          return ProductId + "|" + CategoryId;
      }
  
      #endregion
  }
  </code></pre>
  
  <p>With the corresponding fluent mappings:</p>
  
  <pre><code>public class CategoryMap : ClassMap&lt;Category&gt; {
      public CategoryMap() {
          Table("Categories");
          Id(x =&gt; x.Id);
          Map(x =&gt; x.Name);
      }
  }
  
  public class ProductTopSellerCategoryMap : ClassMap&lt;ProductTopSellerCategory&gt; {
      public ProductTopSellerCategoryMap() {
          Table("ProductTopSellersCategory");
          CompositeId(x =&gt; x.Id)
              .KeyProperty(x =&gt; x.ProductId)
              .KeyProperty(x =&gt; x.CategoryId);
          References(x =&gt; x.Product).ReadOnly();
          References(x =&gt; x.Category).ReadOnly();
          Map(x =&gt; x.Order, "[Order]");
      }
  }
  </code></pre>
  
  <p>Now when I say:</p>
  
  <pre><code>var category = new Category() { Name = "Test 1" };
  var product = session.Get&lt;Product&gt;(1);
  var topSeller = new ProductTopSellerCategory() { Product = product, Category = category };
  
  session.SaveOrUpdate(category);
  
  session.SaveOrUpdate(topSeller);
  
  session.Transaction.Commit();
  </code></pre>
  
  <p>It throws the error:</p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint
    "FK_ProductTopSellersCategory_Categories". The conflict occurred in
    database "xxx", table "dbo.Categories", column 'Id'. The statement has
    been terminated.</p>
  </blockquote>
  
  <p>I've tried to simplify this example as much as possible. I'd really appreciate the help. Thanks</p>