【问题标题】:Nhibernate map by code parent/child/collection insert issuesNhibernate map by code parent/child/collection insert问题
【发布时间】:2016-01-18 20:06:50
【问题描述】:

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

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

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

类别图

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);
        });
    }
}

产品地图

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);
        });
    }
}

实体映射

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.

有人熟悉这个问题吗?我在这里想念什么?我阅读了几个问题和答案,但似乎没有一个有效。

【问题讨论】:

    标签: c# .net nhibernate


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 2021-07-11
      • 1970-01-01
      • 2016-03-22
      相关资源
      最近更新 更多