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
請幫助我。我的問題在哪裏?
是啊! 我的朋友,非常感謝你的幫助。 – 2015-03-19 12:02:47
如果有幫助,很好;)享受強大的NHibernate,先生;) – 2015-03-19 12:04:14