У меня проблема с моей беглостью nhibernate.FluentNhibernate Cascading - не сохранено родительский идентификатор
Мои коды,
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: Entity
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
}
BaseMap:
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. В нормальных условиях, PRODUCTID поле должно быть 1.
Пожалуйста, помогите мне. Где моя проблема?
Да! Мой друг, спасибо вам большое за помощь. –
Если это помогло, отлично;) Наслаждайтесь могучим NHibernate, сэр;) –