简体   繁体   中英

fluent nhibernate delete cascade

I'm using fluent nhibernate, I would turn on delete cascade. but I do not work. it only deletes the foregin key. The following configuration fluent nhibernate:

        public virtual void TreatConfiguration(NHibernate.Cfg.Configuration configuration)
    {
        var update = new SchemaUpdate(configuration);
        update.Execute(false, true);
    }

    public ISessionFactory CreateSessionFactory(string istanza, string db)
    {
        //string server = @"BRUX-PC\SQLEXPRESS";
        //int port = 1433;
        string server = istanza;
        string database = db;
        const string user = "xxxx";
        const string password = "xxxx";

        var connectionString = string.Format("Server={0};Database={1};User Id={2};Password={3};",
                                             server, database, user, password);

        var autoMap = AutoMap.AssemblyOf<AggregateBase>()
                             .Where(t => typeof (AggregateBase).IsAssignableFrom(t))
                             .Conventions.Add(
                                 ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan()),
                                 PrimaryKey.Name.Is(o => "Id"),
                                 ForeignKey.EndsWith("Id"),
                                 DefaultLazy.Never(),
                                 DefaultCascade.All(),
                                 DynamicUpdate.AlwaysTrue(),
                                 DynamicInsert.AlwaysTrue()
            );



        return Fluently.Configure()
                       .Database(
                           MsSqlConfiguration.MsSql2008.ConnectionString(connectionString))
                       .Mappings(m => m.AutoMappings.Add(autoMap))
                       .ExposeConfiguration(TreatConfiguration)
                       .BuildSessionFactory();
    }

my controller :

                        contabilitaRepository.RemoveByIdFattura(testataContabilita.IdFattura);

my repository:

       public void RemoveByIdFattura(Guid? id)
    {
        var userToDelete =
            repository.Single(c => c.IdFattura == id);

        repository
            .Remove(userToDelete);
    }

my model:

    public  class TestataContabilita : AggregateBase
    {
        public virtual Guid IdFattura
        {
            get;
            set;
        }
        public virtual int NumeroRegistrazione
        {
            get;
            set;
        }
        public virtual  string TipoVendita
        {
            get;
            set;
        }

        public virtual IList<CorpoContabilita> CorpoContabilita { get; set; }


    }

     public  class CorpoContabilita : AggregateBase
    {

        public virtual int NumeroRegistrazione
        {
            get;
            set;
        }
        public virtual int? ControPartita
        {
            get;
            set;
        }
        public virtual string Automatico
        {
            get;
            set;
        }
}

when I run the elimination, I only deletes "TestataContabilita" while the "CorpoContabilita" remains, but only deletes the foregin key. why?

You may have to do ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan().Inverse()

in the mapping

Also you have to do

TestataContabilita.CorpoContabilita.Clear();

to remove the items from the collection.

I've tried to reproduce your case. I implemented some kind of a repository and your mapping works just it supposed to. Deletes testataContabilita object with collection of CorpoContabilita.

But I accidentally made a several mistakes in a repository at first - and got null in foreign keys too. In fact they were null even before deleting testataContabilita - due to errors in implementation.

So, what repository implementation do you use? How do you work with Session and Transaction objects? On what object you perform Save? May be you can upload your project somewhere with full implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM