public virtual async Task UpdateSendResults(List <SignalEvent <long> > items) { List <SignalEventLong> mappedList = items .Select(_mapper.Map <SignalEventLong>) .ToList(); string tvpName = TableValuedParameters.GetFullTVPName(_connectionSettings.Schema, TableValuedParameters.EVENT_UPDATE_TYPE); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { MergeCommand <SignalEventLong> update = repository.MergeTVP(mappedList, tvpName); update.Source.IncludeProperty(p => p.SignalEventId) .IncludeProperty(p => p.FailedAttempts) .IncludeProperty(p => p.EventSettingsId) .IncludeProperty(p => p.SubscriberIdRangeFrom) .IncludeProperty(p => p.SubscriberIdRangeTo) .IncludeProperty(p => p.SubscriberIdFromDeliveryTypesHandledSerialized); update.Compare.IncludeProperty(p => p.SignalEventId); update.UpdateMatched .IncludeProperty(p => p.FailedAttempts) .IncludeProperty(p => p.EventSettingsId) .IncludeProperty(p => p.SubscriberIdRangeFrom) .IncludeProperty(p => p.SubscriberIdRangeTo) .IncludeProperty(p => p.SubscriberIdFromDeliveryTypesHandledSerialized); int changes = await update.ExecuteAsync(MergeType.Update) .ConfigureAwait(false); } }
public virtual async Task Update(List <StoredNotification <long> > items) { List <StoredNotificationLong> mappedList = items .Select(_mapper.Map <StoredNotificationLong>) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { string tvpName = TableValuedParameters.GetFullTVPName(_connectionSettings.Schema , TableValuedParameters.STORED_NOTIFICATION_TYPE); MergeCommand <StoredNotificationLong> merge = repository.MergeTVP(mappedList, tvpName); merge.Source .IncludeProperty(x => x.StoredNotificationId) .IncludeProperty(x => x.SubscriberId) .IncludeProperty(x => x.CategoryId) .IncludeProperty(x => x.TopicId) .IncludeProperty(x => x.CreateDateUtc) .IncludeProperty(x => x.MessageSubject) .IncludeProperty(x => x.MessageBody); merge.Compare .IncludeProperty(p => p.StoredNotificationId); merge.UpdateMatched .ExcludeProperty(p => p.StoredNotificationId); int changes = await merge.ExecuteAsync(MergeType.Update).ConfigureAwait(false); } }
public virtual async Task UpsertIsEnabled(List <SubscriberTopicSettingsLong> items) { List <long> disabledTopics = items .Where(x => x.IsEnabled == false) .Select(x => x.SubscriberTopicSettingsId) .ToList(); List <SubscriberTopicSettingsLong> enabledTopics = items .Where(x => x.IsEnabled) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) using (IDbContextTransaction ts = repository.Context.Database.BeginTransaction()) { SqlTransaction sqlTransaction = (SqlTransaction)ts.GetDbTransaction(); if (disabledTopics.Count > 0) { int changes = await repository.DeleteManyAsync <SubscriberTopicSettingsLong>( x => disabledTopics.Contains(x.SubscriberTopicSettingsId)) .ConfigureAwait(false); } if (enabledTopics.Count > 0) { string tvpName = TableValuedParameters.GetFullTVPName(_connectionSettings.Schema , TableValuedParameters.SUBSCRIBER_TOPIC_SETTINGS_TYPE); MergeCommand <SubscriberTopicSettingsLong> merge = repository.MergeTVP(enabledTopics, tvpName, sqlTransaction); merge.Source .IncludeProperty(p => p.TopicId) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.DeliveryType) .IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.AddDateUtc) .IncludeProperty(p => p.IsEnabled); merge.Compare .IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.TopicId) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.DeliveryType); merge.UpdateMatched .IncludeProperty(p => p.IsEnabled); merge.Insert .IncludeProperty(p => p.TopicId) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.DeliveryType) .IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.AddDateUtc) .IncludeProperty(p => p.IsEnabled) .IncludeDefaultValue(p => p.SendCount) .IncludeDefaultValue(p => p.IsDeleted); int changes = await merge.ExecuteAsync(MergeType.Upsert).ConfigureAwait(false); } ts.Commit(); } }
//methods public virtual async Task Insert <TEntity>(List <TEntity> entities) where TEntity : class { //will set ids generated by database on entities using (DbContext dbContext = _dbContextFactory()) { var merge = new MergeCommand <TEntity>(dbContext, entities); merge.Output.SetExcludeAllByDefault(true) .SetExcludeDbGeneratedByDefault(ExcludeOptions.Include); int insertedCount = await merge.ExecuteAsync(MergeType.Insert) .ConfigureAwait(false); } }
public virtual async Task UpsertIsEnabled(List <SubscriberCategorySettingsLong> items) { List <long> disabledCategories = items .Where(x => x.IsEnabled == false) .Select(x => x.SubscriberCategorySettingsId) .ToList(); List <SubscriberCategorySettingsLong> enabledCategories = items .Where(x => x.IsEnabled) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { if (disabledCategories.Count > 0) { int changes = await repository.DeleteManyAsync <SubscriberCategorySettingsLong>( x => disabledCategories.Contains(x.SubscriberCategorySettingsId)) .ConfigureAwait(false); } if (enabledCategories.Count > 0) { string tvpName = TableValuedParameters.GetFullTVPName(_connectionSettings.Schema , TableValuedParameters.SUBSCRIBER_CATEGORY_SETTINGS_TYPE); MergeCommand <SubscriberCategorySettingsLong> merge = repository.MergeTVP(enabledCategories, tvpName); merge.Source .IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.DeliveryType) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.LastSendDateUtc) .IncludeProperty(p => p.SendCount) .IncludeProperty(p => p.IsEnabled); merge.Compare .IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.DeliveryType); merge.UpdateMatched .IncludeProperty(p => p.IsEnabled); merge.Insert .IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.DeliveryType) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.LastSendDateUtc) .IncludeProperty(p => p.SendCount) .IncludeProperty(p => p.IsEnabled); int changes = await merge.ExecuteAsync(MergeType.Upsert) .ConfigureAwait(false); } } }
public virtual async Task UpdateTemplates(List <DispatchTemplate <long> > items, DbContext context, SqlTransaction transaction) { List <DispatchTemplateLong> mappedItems = items .Select(_mapper.Map <DispatchTemplateLong>) .ToList(); MergeCommand <DispatchTemplateLong> merge = new MergeCommand <DispatchTemplateLong>(context, mappedItems, transaction); merge.Compare .IncludeProperty(p => p.DispatchTemplateId); merge.UpdateMatched .ExcludeProperty(x => x.DispatchTemplateId); int changes = await merge.ExecuteAsync(MergeType.Update).ConfigureAwait(false); }
//update public virtual async Task Update(List <DispatchTemplate <long> > items) { List <DispatchTemplateLong> mappedItems = items .Select(_mapper.Map <DispatchTemplateLong>) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { MergeCommand <DispatchTemplateLong> merge = repository.Merge(mappedItems); merge.Compare .IncludeProperty(p => p.DispatchTemplateId); merge.UpdateMatched .ExcludeProperty(x => x.DispatchTemplateId); int changes = await merge.ExecuteAsync(MergeType.Update).ConfigureAwait(false); } }
public virtual async Task Upsert(SubscriberTopicSettingsLong item, bool updateExisting) { using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { MergeCommand <SubscriberTopicSettingsLong> merge = repository.Merge(item); merge.Compare.IncludeProperty(p => p.SubscriberId) .IncludeProperty(p => p.CategoryId) .IncludeProperty(p => p.TopicId); if (updateExisting) { merge.UpdateMatched.IncludeProperty(p => p.IsEnabled) .IncludeProperty(p => p.IsDeleted); } int changes = await merge.ExecuteAsync(MergeType.Upsert) .ConfigureAwait(false); } }
//update public virtual async Task UpdateIsEnabled(List <SubscriberTopicSettingsLong> items) { using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { string tvpName = TableValuedParameters.GetFullTVPName(_connectionSettings.Schema, TableValuedParameters.SUBSCRIBER_TOPIC_SETTINGS_TYPE_IS_ENABLED); MergeCommand <SubscriberTopicSettingsLong> merge = repository.MergeTVP(items, tvpName); merge.Source .IncludeProperty(p => p.SubscriberTopicSettingsId) .IncludeProperty(p => p.IsEnabled); merge.Compare .IncludeProperty(p => p.SubscriberTopicSettingsId); merge.UpdateMatched .IncludeProperty(p => p.IsEnabled); int changes = await merge.ExecuteAsync(MergeType.Update) .ConfigureAwait(false); } }
protected virtual async Task InsertTemplates(List <DispatchTemplate <long> > items , DbContext context, SqlTransaction underlyingTransaction) { List <DispatchTemplateLong> mappedList = items .Select(_mapper.Map <DispatchTemplateLong>) .ToList(); var mergeCommand = new MergeCommand <DispatchTemplateLong>(context, mappedList, underlyingTransaction); mergeCommand.Insert .ExcludeProperty(x => x.DispatchTemplateId); mergeCommand.Output .IncludeProperty(x => x.DispatchTemplateId); int changes = await mergeCommand.ExecuteAsync(MergeType.Insert).ConfigureAwait(false); for (int i = 0; i < mappedList.Count; i++) { items[i].DispatchTemplateId = mappedList[i].DispatchTemplateId; } }
//insert public virtual async Task Insert(List <EventSettings <long> > items) { List <EventSettingsLong> mappedList = items .Select(_mapper.Map <EventSettingsLong>) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) using (IDbContextTransaction ts = repository.Context.Database.BeginTransaction()) { SqlTransaction underlyingTransaction = (SqlTransaction)ts.GetDbTransaction(); MergeCommand <EventSettingsLong> mergeCommand = repository.Merge(mappedList, underlyingTransaction); mergeCommand.Insert .ExcludeProperty(x => x.EventSettingsId) .ExcludeProperty(x => x.TemplatesNavigation); mergeCommand.Output .IncludeProperty(x => x.EventSettingsId); int changes = await mergeCommand.ExecuteAsync(MergeType.Insert).ConfigureAwait(false); for (int i = 0; i < mappedList.Count; i++) { EventSettingsLong mappedItem = mappedList[i]; items[i].EventSettingsId = mappedItem.EventSettingsId; if (mappedItem.Templates != null) { mappedItem.Templates.ForEach( x => x.EventSettingsId = mappedItem.EventSettingsId); } } List <DispatchTemplate <long> > templates = items .SelectMany(x => x.Templates) .ToList(); await InsertTemplates(templates, repository.Context, underlyingTransaction) .ConfigureAwait(false); ts.Commit(); } }
//update public virtual async Task Update(List <EventSettings <long> > items) { List <EventSettingsLong> mappedList = items .Select(_mapper.Map <EventSettingsLong>) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) using (IDbContextTransaction ts = repository.Context.Database.BeginTransaction()) { SqlTransaction underlyingTransaction = (SqlTransaction)ts.GetDbTransaction(); MergeCommand <EventSettingsLong> merge = repository.Merge(mappedList, underlyingTransaction); merge.Compare .IncludeProperty(p => p.EventSettingsId); merge.UpdateMatched .ExcludeProperty(x => x.EventSettingsId); int changes = await merge.ExecuteAsync(MergeType.Update).ConfigureAwait(false); List <DispatchTemplate <long> > templates = items.SelectMany(x => x.Templates).ToList(); await UpdateTemplates(templates, repository.Context, underlyingTransaction).ConfigureAwait(false); ts.Commit(); } }