2015-03-19 57 views
1

我的流利nhibernate有問題。FluentNhibernate級聯 - 未保存父級Id

我的代碼,

BaseEntity:

public abstract class BaseEntity : IEntity<BaseEntity, int>, IDisposable 
{ 
    public virtual int Id { get; set; } 

    public void Dispose() 
    { 
     GC.SuppressFinalize(this); 
    } 
} 

IAuditInfo:

public interface IAuditInfo 
{ 
    int CreatedBy { get; set; } 
    DateTime CreatedDate { get; set; } 
    int UpdatedBy { get; set; } 
    DateTime UpdatedDate { get; set; } 
} 

ISeoFields:

public interface ISeoFields 
{ 
    string MetaKeywords { get; set; } 
    string MetaDescription { get; set; } 
    string MetaTitle { get; set; } 
    string UrlAlias { get; set; } 
} 

產品實體:

public class Product : BaseEntity, IAuditInfo 
{ 
    #region "Constructors" 

    public Product() 
    { 
     Translations = new List<ProductTranslation>(); 
    } 

    #endregion 

    #region "Properties" 

    public virtual IList<ProductTranslation> Translations { get; set; } 

    /******* Implemented from IAuditInfo *******/ 
    public virtual int CreatedBy { get; set; } 
    public virtual DateTime CreatedDate { get; set; } 
    public virtual int UpdatedBy { get; set; } 
    public virtual DateTime UpdatedDate { get; set; } 
    /*******************************************/ 

    #endregion 
}  

ProductTranslation實體:

public class ProductTranslation : BaseEntity, ISeoFields 
{ 
    #region "Constructors" 

    public ProductTranslation() 
    { 
    } 

    #endregion 

    #region "Properties" 

    public virtual Language Language { get; set; } 
    public virtual Product Product { get; set; } 

    public virtual string Name { get; set; } 
    public virtual string ShortName { get; set; } 
    public virtual string Description { get; set; } 

    /******* Implemented from ISeoFields *******/ 
    public virtual string MetaKeywords { get; set; } 
    public virtual string MetaDescription { get; set; } 
    public virtual string MetaTitle { get; set; } 
    public virtual string UrlAlias { get; set; } 
    /*******************************************/ 

    #endregion 
} 

底圖:

public class BaseMap<TEntity, TIdentity> : ClassMap<TEntity> where TEntity : BaseEntity 
{ 
    public BaseMap() 
    { 
     Id<TIdentity>("Id").GeneratedBy.Identity(); 
    } 
} 

ProductMap:

public class ProductMap : BaseMap<Product, int> 
{ 
    public ProductMap() 
    { 
     Map(m => m.CreatedBy); 
     Map(m => m.CreatedDate); 
     Map(m => m.UpdatedBy); 
     Map(m => m.UpdatedDate); 

     HasMany<ProductTranslation>(x => x.Translations).KeyColumns.Add("ProductId").Cascade.All().Inverse(); 
     Table("Product"); 
    } 
} 

ProductTranslationMap:

public class ProductTranslationMap : BaseMap<ProductTranslation, int> 
{ 
    public ProductTranslationMap() 
    { 
     Map(m => m.Name); 
     Map(m => m.ShortName); 
     Map(m => m.Description); 
     Map(m => m.MetaKeywords); 
     Map(m => m.MetaDescription); 
     Map(m => m.MetaTitle); 
     Map(m => m.UrlAlias); 

     References<Language>(r => r.Language, "LanguageId"); 
     References<Product>(r => r.Product, "ProductId"); 

     Table("ProductTranslation"); 
    } 
} 

我節省代碼:

var product = new Product(); 
product.CreatedBy = 1; 
product.CreatedDate = DateTime.Now; 
product.UpdatedBy = 1; 
product.UpdatedDate = DateTime.Now; 
product.Translations = new List<ProductTranslation>() { 
    new ProductTranslation() 
    { 
     Name = "Sony Vaio Notebook", 
     ShortName = "Sony Vaio Notebook Dizüstübilgisayar", 
     Description = "Sony Vaio Notebook Dizüstübilgisayar, falan filan test açıklama", 
     MetaDescription = "Meta Desc Sony Notebook Meta", 
     MetaKeywords = "Sony,Notebook", 
     MetaTitle = "Sony Computer", 
     UrlAlias = "sony-Vaio-notebook", 
     Language = new Language() { 
      Culture ="tr-TR", 
      CurrencyId =1, 
      FlagImage="tr.png", 
      Name="Turkish", 
      Rtl = false 
     } 
    }, 
    new ProductTranslation() 
    { 
     Name = "ASUS N56VZ Notebook", 
     ShortName = "ASUS N56VZ Notebook Dizüstübilgisayar", 
     Description = "ASUS N56VZ Notebook Dizüstübilgisayar, falan filan test açıklama", 
     MetaDescription = "Meta Desc Asus Notebook Meta", 
     MetaKeywords = "ASUS,Notebook", 
     MetaTitle = "ASUS Computer", 
     UrlAlias = "asus-n56vz-notebook", 
     Language = new Language() { 
      Culture ="en-US", 
      CurrencyId = 2, 
      FlagImage="en.png", 
      Name="English", 
      Rtl = false 
     } 
    } 
}; 

Repository.Save(product); 

我的問題,

ProductTable 
    Id | CreatedBy | CreatedDate    | UpdatedBy | UpdatedDate 
    ------------------------------------------------------------------------------- 
    1 | 1   | 2015-03-19 12:42:31.000 | 1   | 2015-03-19 12:42:31.000 

    LanguageTable 
    Id | Name | Culture | FlagImage | Rtl | CurrencyId 
    ------------------------------------------------------------------------------- 
    1 | Turkish | tr-TR | tr.png | 0 | 1 
    2 | English | en-US | en.png | 0 | 2 

    Id | Name    | ShortName   | Description   | MetaKeywords   |MetaDescription   | MetaTitle    | UrlAlias    | LanguageId | ProductId 
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
    1 Sony Vaio Notebook | Sony Vaio Notebook | Dizüstübilgisayar | Sony Vaio Notebook |Dizüstübilgisayar, falan | filan test açıklama | sony-Vaio-notebook | 1   | NULL (Why Null, should be 1) 
    2 ASUS N56VZ Notebook | ASUS N56VZ Notebook | Dizüstübilgisayar | ASUS N56VZ Notebook |Dizüstübilgisayar, falan | filan test açıklama | asus-n56vz-notebook | 2   | NULL (Why Null, should be 1) 

可以看出,LanguageId場得救了。但ProductId未保存在ProductTranslation表中。 在正常情況下,產品編號字段應該是1

請幫助我。我的問題在哪裏?

回答

0

你就要成功了,你只需要分配給它的項目

var product = new SignECommerce.Domain.Entities.Product(); 
... 
product.Translations = new List<ProductTranslation>(); 

// child (collection item) gets reference to parent 
var item1 = new ProductTranslation() 
{ 
    ... 
    Product = product; 
}, 
// now parent is known 
var item2 = new ProductTranslation() 
{ 
    ... 
    Product = product; 
} 

// root of that all is now having children 
product.Translations.Add(item1); 
product.Translations.Add(item2); 

// product save will cause all stuff to be properly persisted 
Repository.Save(product); 

爲什麼的原因,從收集的參考?我們正在使用映射的.Inverse()映射。那種映射的預計:

  • 該項目(集合項)會關心的持久性和
  • ,這樣的項目必須瞭解它的父(收集架)

一般來說,使用ORM/NHibernate,我們應該始終設置引用。父母對孩子和孩子對父母。

當數據從數據庫加載,NHibernate的設置會爲我們......在對象(S)創造它是由我們

+0

是啊! 我的朋友,非常感謝你的幫助。 – 2015-03-19 12:02:47

+0

如果有幫助,很好;)享受強大的NHibernate,先生;) – 2015-03-19 12:04:14