Nhibernate地图由代码父/子/集合插入问题

时间:2016-01-03 13:20:22

标签: c# .net nhibernate

目前我遇到NHibernate的问题,并通过代码映射我的架构。 请参阅Database Schema以了解我要映射的内容。

  • 一个类别至少有一种语言表示
  • 一个类别包含0个或更多产品
  • 一种产品至少有一种语言代表
  • 产品始终属于某个类别

以下是我用来映射这些实体的代码。

CategoryMap

public class CategoryMap : EntityMapping<Category>
{
    public CategoryMap() : base("Category")
    {
        Property(p => p.Status, c =>
          {
              c.Column("Status");
          });

        Set(p => p.CategoryRepresentations, c =>
            {
                c.Lazy(CollectionLazy.NoLazy);
                c.Access(Accessor.NoSetter);
                c.Key(k =>
                {
                    k.Column("CategoryId");
                    k.NotNullable(true);
                });
                c.Cascade(Cascade.All.Exclude(Cascade.DeleteOrphans));                    
            }, o => o.OneToMany());

        Set(p => p.Products, c =>
            {
                c.Lazy(CollectionLazy.Extra);
                c.Access(Accessor.NoSetter);
                c.Key(k =>
                {
                    k.Column("CategoryId");
                    k.NotNullable(true);
                });
                c.Cascade(Cascade.All.Exclude(Cascade.DeleteOrphans));                    
            }, o => o.OneToMany());
    }
}

CategoryRepresentationMap

public class CategoryRepresentationMap : EntityMapping<CategoryRepresentation>
{
    public CategoryRepresentationMap() : base("CategoryRepresentation")
    {
        Property(p=>p.Language, c =>
        {
            c.Column("Language");
        });

        Property(p=>p.RepresentationText, c =>
        {
            c.Column("RepresentationText");
            c.Type(NHibernateUtil.String);
        });
    }
}

ProductMap

public class ProductMap : EntityMapping<Product>
{
    public ProductMap() : base("Product")
    {
        Property(p => p.Status, c =>
        {
            c.Column("Status");
        });

        Set(p => p.ProductRepresentations, c =>
        {
            c.Lazy(CollectionLazy.NoLazy);
            c.Access(Accessor.NoSetter);
            c.Key(k =>
            {
                k.Column("ProductId");
                k.NotNullable(true);
            });
            c.Cascade(Cascade.All.Exclude(Cascade.DeleteOrphans));
            c.Fetch(CollectionFetchMode.Join);
        }, o => o.OneToMany());
    }
}

ProductRepresentationMap

public class ProductRepresentationMap : EntityMapping<ProductRepresentation>
{
    public ProductRepresentationMap() : base("ProductRepresentation")
    {
        Property(p => p.Language, c =>
        {
            c.Column("Language");
        });

        Property(p => p.RepresentationText, c =>
        {
            c.Column("RepresentationText");
            c.Type(NHibernateUtil.String);
        });
    }
}

EntityMapping

public abstract class EntityMapping<TEntity> : ClassMapping<TEntity>
    where TEntity:Entity<long>
{
    protected EntityMapping(string tableName)
    {
        Id(i=>i.Id, c =>
        {
            c.Column("Id");
            c.Generator(Generators.Identity);
            c.Type(NHibernateUtil.Int64);
        });
        Table(tableName);
        DynamicInsert(true);
        DynamicUpdate(true);
    }
}

分类

public class Category : Entity<long>
{
    protected Category()
    {
        _categoryRepresentations=new List<CategoryRepresentation>();
        _products=new List<Product>();
    }

    public static Category Create(Status status, List<CategoryRepresentation> categoryRepresentations)
    {
        var category = new Category()
        {
            Status = status
        };

        foreach (var categoryRepresentation in categoryRepresentations)
        {
            category._categoryRepresentations.Add(categoryRepresentation);
        }

        return category;
    }

    public virtual void AddProduct(Product product)
    {
        _products.Add(product);
    }

    public virtual Status Status { get; set; }

    private readonly ICollection<CategoryRepresentation> _categoryRepresentations;
    public virtual IEnumerable<CategoryRepresentation> CategoryRepresentations
    {
        get
        {
            return _categoryRepresentations;
        }
    }

    private readonly ICollection<Product> _products;

    public virtual IEnumerable<Product> Products
    {
        get
        {
            return _products;
        }
    }
}

当我尝试将产品添加到我的类别时,我会得到这个例外:

    NHibernate.Exceptions.GenericADOException : could not insert: [Catalog.Product][SQL: /* insert Catalog.Product */ INSERT INTO Product (Status, CategoryId) VALUES (?, ?); select SCOPE_IDENTITY()]
  ----> System.Data.SqlClient.SqlException : The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Product_CategoryId". The conflict occurred in database "", table "Category", column 'Id'.
The statement has been terminated.

有谁熟悉这个问题?我在这里错过了什么?我读了几个问题和答案,但似乎都没有。

1 个答案:

答案 0 :(得分:0)

问题已经解决,因为它是与nunit测试和resharper 10.01相结合的问题。更新到10.02解决了它。

相关问题