0

I have a project with fluent-nhibernate. And here is my models and mapping:

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");

    }

My models and mappings are like these. When i run project working normally. But when i call again same method to save database it's adding same data again. I need only update if data is exist.

And here is my source to kisi adding database:

  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;
                }
            }
        }
    }

I run this code first time adding kisi to database and adding hissebilgisi too. But i run this code second time it's update kisi data in database and adding hissebilgisi again. So there will duplicated hissebilgisi in database. I don't want that. I need if need update hissebilgisi too, if there is no hissebilgisi add to database.

What should i do?

Note:I'm sorry my english is not very well i know.

1 Answer 1

1

it should be work

    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;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.