public Employee Update(Employee employeeChanges) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Employee> emp = context.Employees.Attach(employeeChanges); emp.State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); return(employeeChanges); }
public async Task <Joke> Update(Joke joke) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Joke> updated = _dbContext.Jokes.Update(joke); await _dbContext.SaveChangesAsync(); return(updated.Entity); }
public async Task <EntityState> DeleteAsync(T entity) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry removedEntity = _dbContext.Set <T>().Remove(entity); await _dbContext.SaveChangesAsync(); return(removedEntity.State); }
public async Task <User> CreateUser(User user) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <User> trackUser = _configContext.Users.Add(user); await _configContext.SaveChangesAsync(); return(trackUser.Entity); }
public async Task <IActionResult> UploadGroupPhoto(string id, IFormFile file) { if (string.IsNullOrEmpty(id)) { return(NotFound()); } if (file == null || file.Length == 0) { return(Content("file not selected")); } Group group = await _context.Group.SingleOrDefaultAsync(g => g.Id == id); if (group == null) { return(NotFound()); } using (MemoryStream ms = new MemoryStream()) { file.CopyTo(ms); byte[] fileBytes = ms.ToArray(); group.Photo = fileBytes; } Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Group> result = _context.Update(group); return(Ok()); }
private void CreateWithValues(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry dbEntry, string propertyName, object value) { if (dbEntry.Property(propertyName) != null) { dbEntry.Property(propertyName).CurrentValue = value; } }
public static void DbUpdateConcurrencyExceptionHandler(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry, ILogger logger) { var proposedValues = entry.CurrentValues; var databaseValues = entry.GetDatabaseValues(); string msg = ""; foreach (var property in proposedValues.Properties) { Object proposedValue = null; Object databaseValue = null; if (proposedValues?.Properties?.Contains(property) == true) { proposedValue = proposedValues[property]; } if (databaseValues?.Properties?.Contains(property) == true) { databaseValue = databaseValues[property]; } msg += Environment.NewLine + "property: " + property + Environment.NewLine; msg += "\tproposedValue: " + proposedValue + Environment.NewLine; msg += "\tdatabaseValue: " + databaseValue + Environment.NewLine; logger.LogError(msg); // TODO: decide which value should be written to database // proposedValues[property] = <value to be saved>; } throw new NotSupportedException( "Don't know how to handle concurrency conflicts for " + entry.Metadata.Name + ": " + msg); // Refresh original values to bypass next concurrency check entry.OriginalValues.SetValues(databaseValues); }
public async Task <Book> Update(Book book) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Book> updated = _dbContext.Books.Update(book); await _dbContext.SaveChangesAsync(); return(updated.Entity); }
public override async Task PreUpdate(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <PlatformUser> entity) { if (entity.Entity.Password == null) { entity.Property(e => e.Password).IsModified = false; } await Task.CompletedTask; }
protected Patient CreateAndPersistPatient(Agent agent) { Patient patient = CreatePatient(agent.UserAccountId); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Patient> savedPatient = dbContext.Patient.Add(patient); dbContext.SaveChanges(); return(savedPatient.Entity); }
//public BaseRepository() { } #region IBaseRepository<T> 成员 public virtual bool IsInsert(T obj) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <T> entry = BaseDB.BaseDataContext.Entry(obj); return(entry.State == EntityState.Added || entry.State == EntityState.Detached); //TODO:EF5、Core 验证正确性 //return obj.EntityKey == null || obj.EntityKey.EntityKeyValues == null; //entry.IsKeySet }
public void GetChangeType_WhenModelIsNotTracked_ThrowsArgumentException() { ChangeDetectionService cds = MakeService(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(new ModelToAudit()); Assert.Throws <ArgumentException>(() => cds.GetChangeType(entity)); }
public async Task <long> Create(T entity) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <T> result = _applicationContext.Set <T>().Add(entity); await _applicationContext.SaveChangesAsync(); return(result.Entity.Id); }
public void SerializeEntityChanges_WhenChangeTypeInvalid_ThrowsArgumentOutOfRangeException() { ChangeDetectionService cds = MakeService(); ModelToAudit model = GenerateModel(false); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model); Assert.Throws <ArgumentOutOfRangeException>(() => cds.SerializeEntityChanges((AuditedOperationTypeEnum)5, entity)); }
public async Task <Joke> Add(Joke joke) { EnsureId(joke); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Joke> result = await _dbContext.Jokes.AddAsync(joke); await _dbContext.SaveChangesAsync(); return(result.Entity); }
public Product Remove(Product product) { StoreContext storeContext = new StoreContext(); var productRemove = storeContext.Products.SingleOrDefault(products => products.ProductID == product.ProductID); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Product> test = storeContext.Products.Remove(product); storeContext.SaveChanges(); return(product); }
public async Task <Book> Add(Book book) { EnsureId(book); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Book> result = await _dbContext.Books.AddAsync(book); await _dbContext.SaveChangesAsync(); return(result.Entity); }
private static void AddBooleanTraitOption(GetPetDbContext context, Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Trait> traitEntry) { context.TraitOptions.Add(new TraitOption { Option = "כן", FemaleOption = "כן", TraitId = traitEntry.Entity.Id }); context.TraitOptions.Add(new TraitOption { Option = "לא", FemaleOption = "לא", TraitId = traitEntry.Entity.Id }); }
public async Task <Result <Exception, User> > CreateAsync(User user) { user.Validate(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <User> newUser = _context.Users.Add(user); await _context.SaveChangesAsync(); return(newUser.Entity); }
/// <summary> /// Creates a new Node /// </summary> /// <param name="newNode">The Node object to create</param> /// <returns>Returns true if created successfully</returns> public async Task <Node> CreateNodeAsync(Node newNode) { using IServiceScope scope = _serviceScopeFactory.CreateScope(); ApplicationDbContext db = scope.ServiceProvider.GetService <ApplicationDbContext>(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Node> node = db.Nodes.Add(newNode); int changes = await db.SaveChangesAsync(); return(changes > 0 ? node.Entity : null); }
public bool DeleteSubscribe(Supplies relation) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Supplies> subs = Context.Supplies.Remove(relation); if (subs != null) { Context.SaveChanges(); return(true); } return(false); }
public void GetChangeType_WhenModelMemberIsUpdated_ReturnsUpdate() { ChangeDetectionService cds = MakeService(); ModelToAudit model = GenerateModel(); model.Name = Guid.NewGuid().ToString(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model); AuditedOperationTypeEnum changeType = cds.GetChangeType(entity); Assert.AreEqual(AuditedOperationTypeEnum.Update, changeType); }
protected Agent CreateAgent() { Vendor vendor = CreateAndPersistVendor(); // Create Agent Agent agent = CreateAgent(vendor.VendorId); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Agent> savedAgent = dbContext.Agent.Add(agent); dbContext.SaveChanges(); return(savedAgent.Entity); }
public async Task <IActionResult> Create([Bind("BlogId,Url")] Blog blog) { if (ModelState.IsValid) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Blog> myblog = _context.Add(blog); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(blog)); }
public void GetChangeType_WhenModelIsAttachedWithId_ReturnsUpdate() { ChangeDetectionService cds = MakeService(); ModelToAudit model = GenerateModel(false); Context.Attach(model); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model); AuditedOperationTypeEnum changeType = cds.GetChangeType(entity); Assert.AreEqual(AuditedOperationTypeEnum.Update, changeType); }
public override async Task PreUpdate(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <T> entity) { entity.Entity.LastUpdatedTime = DateTime.UtcNow; entity.Entity.LastUpdatedBy = user.Id; entity.Property(x => x.CreatedBy).IsModified = false; entity.Property(x => x.CreatedTime).IsModified = false; foreach (var ignore in ignoreProperties.Value) { entity.Property(ignore).IsModified = false; } await Task.CompletedTask; }
public void SerializeEntityChanges_WhenEntityIsDeleted_SerializesEntityToBefore() { ChangeDetectionService cds = MakeService(); ModelToAudit model = GenerateModel(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model); (string before, string after) = cds.SerializeEntityChanges(AuditedOperationTypeEnum.Delete, entity); Assert.IsNotNull(before); Assert.IsNull(after); }
public bool Subscribe(int?carrierId, int?userId) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Supplies> subscribe = Context.Supplies.Add(new Supplies { CarrierId = carrierId, UserId = userId }); if (subscribe != null) { Context.SaveChanges(); return(true); } return(false); }
public void UpdateAddress(Address address) { address.ModifiedDate = DateTime.Now; Address orig = this.GetOriginal <Address>(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Address> entry = DB.Address.Attach(address); /* * var dbValues = entry.GetDatabaseValues(); * entry.OriginalValues.SetValues(dbValues); */ entry.OriginalValues.SetValues(orig); }
public void Update(Product product) { product.ModifiedDate = DateTime.Now; Product orig = GetOriginal(); Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Product> entry = DB.Product.Attach(product); /* * var dbValues = entry.GetDatabaseValues(); * entry.OriginalValues.SetValues(dbValues); */ entry.OriginalValues.SetValues(orig); }