вот моя настройка nhibernate где я говорю, что не выводить записи у которых "IsDeleted = true" в БД :
public abstract class NHSessionProviderBase {
private ISessionFactory _factory;
private readonly object SyncObject = new object();
private readonly Assembly _entitiesAssembly;
private readonly bool _schemaUpdate;
private readonly FilterDefinition SoftDeleteDefinition;
public string ConnectionString { get; }
public NHSessionProviderBase(string connectionString, Assembly entitiesAssembly, bool schemaUpdate = false) {
SoftDeleteDefinition = new FilterDefinition(
"softdelete",
string.Format("(IsDeleted = :{0})", "isDeleted"),
new Dictionary<string, IType> { { "isDeleted", NHibernateUtil.Boolean } }, true);
ConnectionString = connectionString;
_entitiesAssembly = entitiesAssembly;
_schemaUpdate = schemaUpdate;
}
public ISession OpenSession() {
var session = GetSessionFactory().OpenSession();
ApplyFilters(session);
return session;
}
public IStatelessSession OpenStatelessSession() {
return GetSessionFactory().OpenStatelessSession();
}
private void ApplyFilters(ISession session) {
var type = SoftDeleteDefinition.ParameterTypes.First().Value as PrimitiveType;
session
.EnableFilter(SoftDeleteDefinition.FilterName)
.SetParameter(SoftDeleteDefinition.ParameterNames.First(), type.DefaultValue);
}
private ISessionFactory GetSessionFactory() {
if (_factory == null) {
lock (SyncObject) {
if (_factory == null) {
var configuration = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(ConnectionString)
.IsolationLevel(IsolationLevel.ReadUncommitted);
var mapping = AutoMap.Assembly(_entitiesAssembly)
.Where(x => x.GetInterfaces().Contains(typeof(IBaseEntity)))
.UseOverridesFromAssembly(_entitiesAssembly)
.Conventions.AddAssembly(_entitiesAssembly)
.Conventions.Add<EnumConvention>()
.Conventions.Add<TableNameConvention>()
.Conventions.Add<BinaryColumnLengthConvention>()
.Conventions.Add<CustomForeignKeyConvention>();
AddIgnoredBase(mapping);
AddConventions(mapping.Conventions);
var cfg = Fluently.Configure()
.Database(configuration//.ShowSql()
.UseReflectionOptimizer())
.Mappings(c => c.AutoMappings.Add(mapping))
.ExposeConfiguration(x => x.SetListener(ListenerType.Delete, new SoftDeleteEventListener()))
.ExposeConfiguration(x => x.AddFilterDefinition(SoftDeleteDefinition));
if (_schemaUpdate) {
cfg.ExposeConfiguration(x => new SchemaUpdate(x).Execute(false, true));
}
var build = cfg.BuildConfiguration();
foreach (var classMap in build.ClassMappings) {
if (typeof(IDeletableEntity).IsAssignableFrom(classMap.MappedClass)) {
classMap.AddFilter(SoftDeleteDefinition.FilterName, SoftDeleteDefinition.DefaultFilterCondition);
}
}
_factory = build.BuildSessionFactory();
}
}
}
return _factory;
}
private void AddIgnoredBase(AutoPersistenceModel mapping) {
foreach (var ignored in this.IgnoredBaseClasses()) {
mapping.IncludeBase(ignored);
}
}
private void AddConventions(SetupConventionFinder<AutoPersistenceModel> conventions) {
foreach (var convention in this.CustomConventions()) {
conventions.Add(convention);
}
}
protected virtual Type[] CustomConventions() { return new Type[0]; }
protected virtual Type[] IgnoredBaseClasses() { return new Type[0]; }
}
Тут работает:
var query = _session.Query<Store>(x => x.Organization.Xin == xin).Select(x => new {
x.Name,
x.IsDeleted
});
Но когда делаешь SelectMany, оно выводит удаленные записи:
var q = _session.Query<Organization>(x => x.Xin == xin).SelectMany(x => x.Stores).Select(x => new {
x.IsDeleted,
x.Name
});
может кому-то поможет :
public class OrganizationOverride : IAutoMappingOverride<Organization> {
public void Override(AutoMapping<Organization> mapping) {
mapping.HasMany(x => x.Stores).ApplyFilter<SoftDeleteDefinition>();
}
}
и сам фильтр:
public class SoftDeleteDefinition : FilterDefinition {
public SoftDeleteDefinition() {
this.WithName("mappingSoftDelete");
this.WithCondition(string.Format("(IsDeleted = :{0})", "isDeleted"));
this.AddParameter("isDeleted", NHibernateUtil.Boolean);
}
}
Виртуальный выделенный сервер (VDS) становится отличным выбором
Подскажите, как можно подсчитать количество символов в поле типа TEXT в БД PostgreSQL, подсчитать количество символов без пробелов, HTML-ссылок, спецсимволов?
Есть данные json, полученные с сервераПытаюсь вывести их в виде дерева на страничку html