Fluent-NHibernate 向数据库 MVC 添加了多个数据
Fluent-NHibernate added Multiple Data To Database MVC
我有一个使用 fluent-nhibernate 的项目。这是我的模型和映射:
TapuKisiKisi.cs:
public class TapuKisiModel : SModuleClass
{
public virtual int Kod { get; set; }
public virtual long TapuKod { get; set; }
public virtual string Ad { get; set; }
public virtual TapuKisiTipModel KisiTip { get; set; }
[JsonIgnore]
public virtual IList<TapuHisseBilgisiModel> HisseBilgisi { get; set; }
public virtual TapuTuzelKisiModel TuzelKisi { get; set; }
public virtual TapuGercekKisiModel GercekKisi { get; set; }
public TapuKisiModel()
{
HisseBilgisi = new List<TapuHisseBilgisiModel>();
}
}
TapuKisiModelMap.cs:
public class TapuKisiModelMap : ClassMap<TapuKisiModel>
{
public TapuKisiModelMap()
{
Table("TAPU_KISI");
Id(x => x.Kod);
Map(x => x.Ad);
Map(x => x.TapuKod);
Map(x => x.Record).Length(25).Nullable();
Map(x => x.RecordDate).Nullable();
Map(x => x.Edit).Length(25);
Map(x => x.EditDate);
References(x => x.KisiTip).PropertyRef(x => x.TapuKod).Column("CinsiyetTapuKod").Index("IX_KISI_CINSIYETTAPUKOD");
References(x => x.GercekKisi).Column("GercekKisiKod").Cascade.SaveUpdate().Index("IX_KISI_GERCEKKISIKOD");
References(x => x.TuzelKisi).Column("TuzelKisiKod").Cascade.SaveUpdate().Index("IX_KISI_TUZELKISIKOD");
HasMany(x => x.HisseBilgisi).PropertyRef("TapuKod").KeyColumn("KisiTapuKod").Cascade.SaveUpdate();
}
}
TapuHisseBilgisiModel.cs:
public partial class TapuHisseBilgisiModel : SModuleClass
{
public virtual int Kod { get; set; }
public virtual long TapuKod { get; set; }
public virtual string EdinmeSebep { get; set; }
public virtual decimal HissePay { get; set; }
public virtual decimal HissePayda { get; set; }
public virtual TapuIslemModel Islem { get; set; }
}
TapuHisseBilgisiModelMap.cs:
public TapuHisseBilgisiModelMap()
{
Table("TAPU_HISSE_BILGISI");
Id(x => x.Kod);
Map(x => x.TapuKod);
Map(x => x.EdinmeSebep);
Map(x => x.HissePay);
Map(x => x.HissePayda);
Map(x => x.Record).Length(25).Nullable();
Map(x => x.RecordDate).Nullable();
Map(x => x.Edit).Length(25);
Map(x => x.EditDate);
HasOne(x => x.Islem).ForeignKey("HisseBilgisiTapuKod").PropertyRef(x => x.HisseBilgisi);
References(x => x.Kisi).PropertyRef(x => x.TapuKod).Column("KisiTapuKod").Index("IX_HISSEBILGISI_KISITAPUKOD");
References(x => x.Zemin).PropertyRef(x => x.TapuKod).Column("ZeminTapuKod").Index("IX_HISSEBILGISI_ZEMINTAPUKOD");
}
我的模型和映射是这样的。当我 运行 项目正常工作时。但是当我再次调用相同的方法来保存数据库时,它会再次添加相同的数据。我只需要在数据存在时更新。
这是我添加数据库的 kisi 来源:
internal bool AddKisiToDatabase(List<TapuKisiModel> kisiList)
{
using (ISession session = DatabaseProvider.SessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
try
{
foreach (var kisi in kisiList)
{
TapuKisiModel currentKisi = session
.QueryOver<TapuKisiModel>()
.Where(x => x.TapuKod == kisi.TapuKod)
.SingleOrDefault();
if (currentKisi != null)
{
currentKisi.TapuKod = kisi.TapuKod;
foreach (var hisse in kisi.HisseBilgisi)
{
currentKisi.HisseBilgisi.Add(hisse);
}
session.Update(currentKisi);
}
else
{
session.Save(kisi);
}
}
transaction.Commit();
return true;
}
catch (Exception ex)
{
SNLog.SLogger.Error(string.Format("{0} - {1}", "An Error Occurred While Adding tuzelKisiList To Database", ex.Message));
return false;
}
}
}
}
我 运行 此代码是第一次将 kisi 添加到数据库并添加 hissebilgisi。但是我 运行 这段代码是第二次更新数据库中的 kisi 数据并再次添加 hissebilgisi。所以数据库中会有重复的 hissebilgisi。我不想要那个。我也需要更新 hissebilgisi,如果没有 hissebilgisi 添加到数据库。
我该怎么办?
注意:对不起,我的英语不是很好。
应该可以了
internal bool AddKisiToDatabase(List<TapuKisiModel> kisiList)
{
bool state = false;
using (ISession session = DatabaseProvider.SessionFactory.OpenSession())
{
try
{
using (ITransaction transaction = session.BeginTransaction())
{
foreach (var kisi in kisiList)
{
TapuKisiModel currentKisi = new TapuKisiModel();
currentKisi = session
.QueryOver<TapuKisiModel>()
.Where(x => x.TapuKod == kisi.TapuKod)
.SingleOrDefault();
session.SaveOrUpdate(currentKisi);
transaction.Commit();
}
}
state = true;
}
catch (Exception ex)
{
SNLog.SLogger.Error(string.Format("{0} - {1}", "An Error Occurred While Adding tuzelKisiList To Database", ex.Message));
}
}
return state;
}
我有一个使用 fluent-nhibernate 的项目。这是我的模型和映射:
TapuKisiKisi.cs:
public class TapuKisiModel : SModuleClass
{
public virtual int Kod { get; set; }
public virtual long TapuKod { get; set; }
public virtual string Ad { get; set; }
public virtual TapuKisiTipModel KisiTip { get; set; }
[JsonIgnore]
public virtual IList<TapuHisseBilgisiModel> HisseBilgisi { get; set; }
public virtual TapuTuzelKisiModel TuzelKisi { get; set; }
public virtual TapuGercekKisiModel GercekKisi { get; set; }
public TapuKisiModel()
{
HisseBilgisi = new List<TapuHisseBilgisiModel>();
}
}
TapuKisiModelMap.cs:
public class TapuKisiModelMap : ClassMap<TapuKisiModel>
{
public TapuKisiModelMap()
{
Table("TAPU_KISI");
Id(x => x.Kod);
Map(x => x.Ad);
Map(x => x.TapuKod);
Map(x => x.Record).Length(25).Nullable();
Map(x => x.RecordDate).Nullable();
Map(x => x.Edit).Length(25);
Map(x => x.EditDate);
References(x => x.KisiTip).PropertyRef(x => x.TapuKod).Column("CinsiyetTapuKod").Index("IX_KISI_CINSIYETTAPUKOD");
References(x => x.GercekKisi).Column("GercekKisiKod").Cascade.SaveUpdate().Index("IX_KISI_GERCEKKISIKOD");
References(x => x.TuzelKisi).Column("TuzelKisiKod").Cascade.SaveUpdate().Index("IX_KISI_TUZELKISIKOD");
HasMany(x => x.HisseBilgisi).PropertyRef("TapuKod").KeyColumn("KisiTapuKod").Cascade.SaveUpdate();
}
}
TapuHisseBilgisiModel.cs:
public partial class TapuHisseBilgisiModel : SModuleClass
{
public virtual int Kod { get; set; }
public virtual long TapuKod { get; set; }
public virtual string EdinmeSebep { get; set; }
public virtual decimal HissePay { get; set; }
public virtual decimal HissePayda { get; set; }
public virtual TapuIslemModel Islem { get; set; }
}
TapuHisseBilgisiModelMap.cs:
public TapuHisseBilgisiModelMap()
{
Table("TAPU_HISSE_BILGISI");
Id(x => x.Kod);
Map(x => x.TapuKod);
Map(x => x.EdinmeSebep);
Map(x => x.HissePay);
Map(x => x.HissePayda);
Map(x => x.Record).Length(25).Nullable();
Map(x => x.RecordDate).Nullable();
Map(x => x.Edit).Length(25);
Map(x => x.EditDate);
HasOne(x => x.Islem).ForeignKey("HisseBilgisiTapuKod").PropertyRef(x => x.HisseBilgisi);
References(x => x.Kisi).PropertyRef(x => x.TapuKod).Column("KisiTapuKod").Index("IX_HISSEBILGISI_KISITAPUKOD");
References(x => x.Zemin).PropertyRef(x => x.TapuKod).Column("ZeminTapuKod").Index("IX_HISSEBILGISI_ZEMINTAPUKOD");
}
我的模型和映射是这样的。当我 运行 项目正常工作时。但是当我再次调用相同的方法来保存数据库时,它会再次添加相同的数据。我只需要在数据存在时更新。
这是我添加数据库的 kisi 来源:
internal bool AddKisiToDatabase(List<TapuKisiModel> kisiList)
{
using (ISession session = DatabaseProvider.SessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
try
{
foreach (var kisi in kisiList)
{
TapuKisiModel currentKisi = session
.QueryOver<TapuKisiModel>()
.Where(x => x.TapuKod == kisi.TapuKod)
.SingleOrDefault();
if (currentKisi != null)
{
currentKisi.TapuKod = kisi.TapuKod;
foreach (var hisse in kisi.HisseBilgisi)
{
currentKisi.HisseBilgisi.Add(hisse);
}
session.Update(currentKisi);
}
else
{
session.Save(kisi);
}
}
transaction.Commit();
return true;
}
catch (Exception ex)
{
SNLog.SLogger.Error(string.Format("{0} - {1}", "An Error Occurred While Adding tuzelKisiList To Database", ex.Message));
return false;
}
}
}
}
我 运行 此代码是第一次将 kisi 添加到数据库并添加 hissebilgisi。但是我 运行 这段代码是第二次更新数据库中的 kisi 数据并再次添加 hissebilgisi。所以数据库中会有重复的 hissebilgisi。我不想要那个。我也需要更新 hissebilgisi,如果没有 hissebilgisi 添加到数据库。
我该怎么办?
注意:对不起,我的英语不是很好。
应该可以了
internal bool AddKisiToDatabase(List<TapuKisiModel> kisiList)
{
bool state = false;
using (ISession session = DatabaseProvider.SessionFactory.OpenSession())
{
try
{
using (ITransaction transaction = session.BeginTransaction())
{
foreach (var kisi in kisiList)
{
TapuKisiModel currentKisi = new TapuKisiModel();
currentKisi = session
.QueryOver<TapuKisiModel>()
.Where(x => x.TapuKod == kisi.TapuKod)
.SingleOrDefault();
session.SaveOrUpdate(currentKisi);
transaction.Commit();
}
}
state = true;
}
catch (Exception ex)
{
SNLog.SLogger.Error(string.Format("{0} - {1}", "An Error Occurred While Adding tuzelKisiList To Database", ex.Message));
}
}
return state;
}