private static void SetupRole() { OrmConfiguration.GetDefaultEntityMapping <Role>() .SetTableName("Role") .SetProperty(role => role.Id, prop => prop.SetPrimaryKey().SetDatabaseGenerated(DatabaseGeneratedOption.Identity)); }
public async Task <QuyenTacVu> UpdatePartial(QuyenTacVu quyen, params string[] field) { return(await WithConnection(async c => { QuyenTacVu obj = await c.GetAsync(quyen); if (obj == null) { await c.InsertAsync(quyen); if (quyen.ChucNangId == 0) { throw new Exception("Insert Fail"); } return quyen; } var list = field.ToList(); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <QuyenTacVu>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = await c.UpdateAsync(quyen, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return quyen; })); }
public async Task <KhoHangHoa> UpdatePartial(KhoHangHoa khachhang, params string[] field) { var a = await WithConnection(async c => { KhoHangHoa obj = await c.GetAsync(khachhang); if (obj == null) { throw new Exception(string.Format("Update id {0} not exist", khachhang.HangHoaId.ToString())); } var list = field.ToList(); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <KhoHangHoa>() .Clone() .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = await c.UpdateAsync(khachhang, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return(khachhang); }); return(a); }
public void WhenIPartiallyUpdateAllTheInsertedEmployeeEntities() { // prepare a new mapping var defaultMapping = OrmConfiguration.GetDefaultEntityMapping <Employee>(); Assert.IsTrue(defaultMapping.IsFrozen); var lastNamePropMapping = defaultMapping.GetProperty(employee => employee.LastName); try { // updates are not possible when the mapping is frozen defaultMapping.SetProperty(employee => employee.LastName, propMapping => propMapping.ExcludeFromUpdates()); Assert.Fail(); } catch (InvalidOperationException) { } var customMapping = defaultMapping.Clone().UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(Employee.LastName), nameof(Employee.FullName)); for (var entityIndex = 0; entityIndex < _testContext.LocalEntities.Count; entityIndex++) { var insertedEntity = _testContext.LocalEntities[entityIndex] as Employee; if (insertedEntity == null) { continue; } var partialUpdatedEntity = new Employee() { UserId = insertedEntity.UserId, EmployeeId = insertedEntity.EmployeeId, BirthDate = new DateTime(2020, 03, 01), WorkstationId = 10 + insertedEntity.WorkstationId, FirstName = "Updated " + insertedEntity.FirstName, // all of the above will be ignored with the exception of the next ones LastName = "Updated " + insertedEntity.LastName }; _testContext.DatabaseConnection.Update(partialUpdatedEntity, statement => statement.WithEntityMappingOverride(customMapping)); _testContext.LocalEntities[entityIndex] = new Employee() { UserId = insertedEntity.UserId, KeyPass = insertedEntity.KeyPass, EmployeeId = insertedEntity.EmployeeId, BirthDate = insertedEntity.BirthDate, WorkstationId = insertedEntity.WorkstationId, FirstName = insertedEntity.FirstName, // all of the above were ignored with the exception of the next ones LastName = partialUpdatedEntity.LastName, FullName = partialUpdatedEntity.FullName }; } }
internal void Save(IDbConnection db) { if (!_licensesToRemove.IsEmpty) { var idsToRemove = new StringBuilder(); var firstRun = true; License licenseToRemove; while (_licensesToRemove.TryPop(out licenseToRemove)) { if (firstRun) { firstRun = false; } else { idsToRemove.Append(','); } idsToRemove.Append(licenseToRemove.Id); } db.BulkDelete <PlayerLicenseDto>(statement => statement .Where($"{nameof(PlayerLicenseDto.Id):C} IN ({idsToRemove})")); } foreach (var license in this) { if (!license.ExistsInDatabase) { db.Insert(new PlayerLicenseDto { Id = license.Id, PlayerId = (int)_player.Account.Id, License = (byte)license.ItemLicense, FirstCompletedDate = license.FirstCompletedDate.ToUnixTimeSeconds(), CompletedCount = license.TimesCompleted }); license.ExistsInDatabase = true; } else { if (!license.NeedsToSave) { continue; } var mapping = OrmConfiguration .GetDefaultEntityMapping <PlayerLicenseDto>() .Clone() .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(PlayerLicenseDto.CompletedCount)); db.Update(new PlayerLicenseDto { CompletedCount = license.TimesCompleted }, statement => statement.WithEntityMappingOverride(mapping)); license.NeedsToSave = false; } } }
static XtcsModel() { OrmConfiguration.GetDefaultEntityMapping <XtcsModel>() .SetProperty(entity => entity.Id, prop => { prop.SetDatabaseColumnName("Xtcsdm00"); }); }
static BaseDapperFastCrud() { InfoMapping.Add(Common.SettingItem.DBType.MSSQL, OrmConfiguration.GetDefaultEntityMapping <T>() .Clone().SetDialect(SqlDialect.MsSql)); InfoMapping.Add(Common.SettingItem.DBType.POSTGRESQL, OrmConfiguration.GetDefaultEntityMapping <T>() .Clone().SetDialect(SqlDialect.PostgreSql)); }
private static void SetupUser() { OrmConfiguration.GetDefaultEntityMapping <User>() .SetTableName("User") .SetProperty(user => user.Id, prop => prop.SetPrimaryKey().SetDatabaseGenerated(DatabaseGeneratedOption.Identity)) .SetProperty(user => user.RoleId, prop => prop.SetChildParentRelationship <Role>("Role")); }
public static void SetDialogueIfNeeded_AddsMappedIsFroozenToDictionary() { var target = new SqlDialectHelper(); target.SetDialogueIfNeeded <Brave>(SqlDialect.SqLite); var result = target.GetEntityState <Brave>(); Assert.That(result.HasValue, Is.True); Assert.That(OrmConfiguration.GetDefaultEntityMapping <Brave>().Dialect, Is.EqualTo(SqlDialect.SqLite)); }
static DepartmentModel() { OrmConfiguration.GetDefaultEntityMapping <DepartmentModel>() .SetProperty(entity => entity.Id, prop => { prop.SetDatabaseColumnName("Did"); prop.SetDatabaseGenerated(DatabaseGeneratedOption.Identity); }); }
static Arz0Model() { OrmConfiguration.GetDefaultEntityMapping <Arz0Model>() .SetProperty(entity => entity.Id, prop => { prop.SetDatabaseColumnName("Arz0xh00"); prop.SetDatabaseGenerated(DatabaseGeneratedOption.Identity); }); }
static ExtendTypeItemModel() { OrmConfiguration.GetDefaultEntityMapping <ExtendTypeItemModel>() .SetProperty(entity => entity.Id, prop => { prop.SetDatabaseColumnName("Id"); prop.SetDatabaseGenerated(DatabaseGeneratedOption.Identity); }); }
public static void SetDialogueIfNeeded_SetsIsFroozenInDictionary() { var repo = new BraveRepository(Factory); repo.GetKey <ITestSession>(1); var target = new SqlDialectHelper(); var result = target.GetEntityState <Brave>(); Assert.That(result.HasValue, Is.True); Assert.That(result.Value, Is.True); Assert.That(OrmConfiguration.GetDefaultEntityMapping <Brave>().Dialect, Is.EqualTo(SqlDialect.SqLite)); }
public void DisableAllVariants(string productId) { var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <TiqetVariantDetail>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(TiqetVariantDetail.IsEnabled)); GetCurrentConnection().BulkUpdate( new TiqetVariantDetail { IsEnabled = false, }, statement => statement.WithEntityMappingOverride(partialUpdateMapping).Where($"{nameof(TiqetVariantDetail.ProductId):C}=@ProductId").WithParameters(new { ProductId = productId }).AttachToTransaction(GetCurrentTransaction())); }
public void DisableAllDatesByEventId(long eventId) { var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <PlaceHolidayDate>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(PlaceHolidayDate.IsEnabled)); GetCurrentConnection().BulkUpdate( new PlaceHolidayDate { IsEnabled = false, // You can add as many as fields you want to update }, statement => statement.WithEntityMappingOverride(partialUpdateMapping).Where($"{nameof(PlaceHolidayDate.EventId):C}=@EventId").WithParameters(new { EventId = eventId }).AttachToTransaction(GetCurrentTransaction())); }
public static void Table_Will_SetDialect() { var connection = CreateSession(null); var sql = SqlInstance.Instance; connection.Query($"SELECT * FROM {sql.Table<Brave>(connection.SqlDialect)}"); var target = new SqlDialectHelper(); var result = target.GetEntityState <Brave>(); Assert.That(result.HasValue, Is.True); Assert.That(result.Value, Is.True); Assert.That((int)OrmConfiguration.GetDefaultEntityMapping <Brave>().Dialect, Is.EqualTo((int)connection.SqlDialect)); }
public async Task CreateNickHandler(GameServer server, GameSession session, CCreateNickReqMessage message) { if (session.Player == null || !string.IsNullOrWhiteSpace(session.Player.Account.Nickname)) { session.Dispose(); return; } Logger.Info() .Account(session) .Message($"Creating nickname {message.Nickname}") .Write(); if (!await IsNickAvailableAsync(message.Nickname).ConfigureAwait(false)) { Logger.Error() .Account(session) .Message($"Nickname not available: {message.Nickname}") .Write(); await session.SendAsync(new SCheckNickAckMessage(false)) .ConfigureAwait(false); return; } session.Player.Account.Nickname = message.Nickname; using (var db = AuthDatabase.Open()) { var mapping = OrmConfiguration .GetDefaultEntityMapping <AccountDto>() .Clone() .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(AccountDto.Nickname)); await db.UpdateAsync(new AccountDto { Id = (int)session.Player.Account.Id, Nickname = message.Nickname }, statement => statement.WithEntityMappingOverride(mapping)) .ConfigureAwait(false); } //session.Send(new SCreateNickAckMessage { Nickname = msg.Nickname }); await session.SendAsync(new SServerResultInfoAckMessage(ServerResult.CreateNicknameSuccess)) .ConfigureAwait(false); Logger.Info() .Account(session) .Message($"Created nickname {message.Nickname}") .Write(); await LoginAsync(server, session) .ConfigureAwait(false); }
public void DisableAll() { var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <TiqetEventDetailMapping>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(TiqetEventDetailMapping.IsEnabled)); GetCurrentConnection().BulkUpdate( new TiqetEventDetailMapping { IsEnabled = false, }, statement => statement.WithEntityMappingOverride(partialUpdateMapping) .AttachToTransaction(GetCurrentTransaction())); }
static void Main(string[] args) { var serviceProvider = new ServiceCollection() .AddSingleton <IBaseRepostory <Bank>, BankRepository>() .AddScoped <IDbConnection>(_ => new MySqlConnection("Server=172.17.0.1;DataBase=testedb;Uid=root;Pwd=example")) .AddScoped(_ => OrmConfiguration .GetDefaultEntityMapping <Bank>() .SetDialect(SqlDialect.MySql) .SetTableName("bank") ) .AddSingleton <Service>() .BuildServiceProvider(); serviceProvider.GetService <Service>().Run(); }
public static void Query_Wont_MakeFrozen() { var connection = new TestSessionMemory(A.Fake <IDbFactory>()); new MigrateDb(connection); var target = new SqlDialectHelper(); target.Reset(); OrmConfiguration.RegisterEntity <Brave>(); connection.Query("SELECT * FROM Braves"); var result = target.GetEntityState <Brave>(); Assert.That(OrmConfiguration.GetDefaultEntityMapping <Brave>().IsFrozen, Is.False); Assert.That(result.HasValue, Is.False); }
public void DisableAll() { var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <AlgoliaExport>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, nameof(AlgoliaExport.IsIndexed)); GetCurrentConnection().BulkUpdate( new AlgoliaExport { IsIndexed = false, }, statement => statement.WithEntityMappingOverride(partialUpdateMapping) .Where($"{nameof(AlgoliaExport.IsEnabled):C}=1") .AttachToTransaction(GetCurrentTransaction())); }
public static void RegisterEntities() { OrmConfiguration.DefaultDialect = SqlDialect.MsSql; OrmConfiguration.GetDefaultEntityMapping <UserDataModel>() .SetTableName("brg_Users") .SetProperty(user => user.Id, prop => prop .SetPrimaryKey() .SetDatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)); OrmConfiguration.GetDefaultEntityMapping <OrderDataModel>() .SetTableName("brg_Orders") .SetProperty(user => user.Id, prop => prop .SetPrimaryKey().SetDatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)); Migrate(); }
private void SetDialectIfDialogIncorrect(SqlDialect dialect) { _mapping = OrmConfiguration.GetDefaultEntityMapping <TEntity>(); if (_mapping.Dialect == dialect) { return; } lock (LockSqlDialectUpdate) { _mapping = OrmConfiguration.GetDefaultEntityMapping <TEntity>(); if (_mapping.Dialect == dialect) { return; } _mapping.SetDialect(dialect); } }
static KrzlModel() { OrmConfiguration.GetDefaultEntityMapping <KrzlModel>() .SetProperty(entity => entity.Id, prop => { prop.SetDatabaseColumnName("Krzlzh00"); prop.SetDatabaseGenerated(DatabaseGeneratedOption.Identity); }); //OrmConfiguration.GetDefaultEntityMapping<KrzlModel>() // .SetProperty(entity => entity.Krzlaazd, // prop => // { // prop.IsExcludedFromUpdates = true; // prop.IsExcludedFromInserts = true; // }); }
private static void SetDialogueIfNeeded <TEntity>(SqlDialect sqlDialect) { var mapping = OrmConfiguration.GetDefaultEntityMapping <TEntity>(); if (mapping.IsFrozen || mapping.Dialect == sqlDialect) { return; } lock (LockSqlDialectUpdate) { mapping = OrmConfiguration.GetDefaultEntityMapping <TEntity>(); //reload to be sure if (mapping.IsFrozen || mapping.Dialect == sqlDialect) { return; } mapping.SetDialect(sqlDialect); } }
public async Task <DuAn> UpdatePartial(DuAn duan, params string[] field) { var a = await WithConnection(async c => { DuAn obj = await c.GetAsync(duan); if (obj == null) { throw new Exception(string.Format("Update id {0} not exist", duan.DuAnId.ToString())); } if (obj.CtrVersion != duan.CtrVersion) { throw new Exception(string.Format("Update id {0} have version confict" , duan.DuAnId.ToString())); } duan.CtrVersion += 1; var list = field.ToList(); list.Add(nameof(duan.CtrVersion)); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <DuAn>() .Clone() .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = await c.UpdateAsync(duan, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return(duan); }); return(a); }
public async Task <LoaiNghiPhep> UpdatePartial(LoaiNghiPhep entity, params string[] field) { return(await WithConnection(async c => { LoaiNghiPhep obj = await c.GetAsync(entity); if (obj == null) { throw new Exception(string.Format("Update id {0} not exist", entity.LoaiNghiPhepId.ToString())); } if (obj.CtrVersion != entity.CtrVersion) { throw new Exception(string.Format("Update id {0} have version confict" , entity.LoaiNghiPhepId.ToString())); } entity.CtrVersion += 1; var list = field.ToList(); list.Add(nameof(LoaiNghiPhep.CtrVersion)); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <LoaiNghiPhep>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = await c.UpdateAsync(entity, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return entity; })); }
public async Task <QuanLyHopDong> UpdatePartialBase(QuanLyHopDong model, bool checkCtrVersion, params string[] field) { return(await WithConnection(async c => { var obj = await c.GetAsync(model); if (obj == null) { throw new Exception(string.Format("Update id {0} not exist", model.QuanLyHopDongId.ToString())); } if (checkCtrVersion == true && obj.CtrVersion != model.CtrVersion) { throw new Exception(string.Format("Update id {0} have version confict" , model.QuanLyHopDongId.ToString())); } model.CtrVersion += 1; var list = field.ToList(); list.Add(nameof(QuanLyHopDong.CtrVersion)); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <QuanLyHopDong>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = await c.UpdateAsync(model, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return model; })); }
public NguoiDung UpdatePartial(NguoiDung nguoidung, params string[] field) { return(WithConnection(c => { NguoiDung obj = c.Get(nguoidung); if (obj == null) { throw new Exception(string.Format("Update id {0} not exist", nguoidung.NguoiDungId.ToString())); } if (obj.CtrVersion != nguoidung.CtrVersion) { throw new Exception(string.Format("Update id {0} have version confict" , nguoidung.NguoiDungId.ToString())); } nguoidung.CtrVersion += 1; var list = field.ToList(); list.Add(nameof(NguoiDung.CtrVersion)); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <NguoiDung>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = c.Update(nguoidung, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return nguoidung; })); }
public Client UpdatePartial(Client client, params string[] field) { return(WithConnection(c => { Client obj = c.Get(client); if (obj == null) { throw new Exception(string.Format("Update id {0} not exist", client.ClientId)); } if (obj.CtrVersion != client.CtrVersion) { throw new Exception(string.Format("Update id {0} have version confict" , client.ClientId)); } client.CtrVersion += 1; var list = field.ToList(); list.Add(nameof(Client.CtrVersion)); var partialUpdateMapping = OrmConfiguration .GetDefaultEntityMapping <Client>() .Clone() // clone it if you don't want to modify the default .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true, list.ToArray()); var result = c.Update(client, statement => statement.WithEntityMappingOverride(partialUpdateMapping)); if (result != true) { throw new Exception("Update Fail"); } return client; })); }