我正在使用流利的 nhibernate,我会打开删除级联。但我不工作。它只删除了foregin键。下面配置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();
}
我的控制器:
contabilitaRepository.RemoveByIdFattura(testataContabilita.IdFattura);
我的存储库:
public void RemoveByIdFattura(Guid? id)
{
var userToDelete =
repository.Single(c => c.IdFattura == id);
repository
.Remove(userToDelete);
}
我的模型:
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;
}
}
当我运行消除时,我只删除了“TestataContabilita”,而“CorpoContabilita”仍然存在,但只删除了外键。为什么?