NHibernate Session.Save与Ids父子关系

时间:2016-01-25 11:58:00

标签: c# nhibernate fluent-nhibernate

我有以下情况,我希望保存父亲 - 儿童的活动:

public Product()
    {
        this.Lines = new Collection<Line>();
        this.Parameters = new Collection<ProductParameter>();
        this.ResourceLink = new List<ResourceLink>();
    }

    public virtual string Code { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual ICollection<ProductParameter> Parameters { get; set; }

并且

public class ProductParameter : EntityBase<Guid>
{
    public virtual string Name { get; set; }
    public virtual string Value { get; set; }
    public virtual string Description { get; set; }
    public virtual Product Product { get; set; }
}

使用映射

 public void Override(AutoMapping<ProductParameter> mapping)
    {
        mapping.Id(x => x.Id).Unique().GeneratedBy.Guid();
        mapping.Map(x => x.Name).Not.Nullable();
        mapping.Map(x => x.Description);
        mapping.Map(x => x.Value);
        mapping.References(m => m.Product);
        mapping.References(m => m.Station);
    }

public void Override(AutoMapping<Product> mapping)
    {
        mapping.Id(x => x.Id).Unique().GeneratedBy.Guid();
        mapping.Map(x => x.Name).Not.Nullable();
        mapping.Map(x => x.Code).Not.Nullable();
        mapping.HasMany(m => m.Parameters).Cascade.All();

    }

public void Override(AutoMapping<ProductParameter> mapping)
    {
        mapping.Id(x => x.Id).Unique().GeneratedBy.Guid();
        mapping.Map(x => x.Name).Not.Nullable();
        mapping.Map(x => x.Description);
        mapping.Map(x => x.Value);
        mapping.References(m => m.Product);
    }

由于某些特定原因,我需要在某些情况下,实体ID是自动生成的(新产品),而在其他情况下,我需要保存具有已分配ID的实体(其他应用程序设置Guid与Guid不同)。空)。 对于最后这些情况,我使用以下代码:

/// Save an entity w
    /// </summary>
    /// <param name="domainObject">The domain object.</param>
    /// <returns></returns>
    public T ForceSave(T domainObject)
    {
        domainObject.DateCreated = DateTime.Now;

        domainObject.DateModified = DateTime.Now;

        this.Session.Save(domainObject,domainObject.Id);
        if (!Session.Transaction.IsActive)
        {
            this.Session.Flush();
            this.Session.Evict(domainObject);
        }

        return domainObject;
    }

Onec调用Flush执行我收到批量更新错误(由于参数)。实体产品保存正确但不保存。

你知道为什么吗?

提前致谢!

0 个答案:

没有答案