savechanges

How to get id from entity for Auditlog in Entity Framework 6

醉酒当歌 提交于 2019-12-03 08:26:56
问题 I know it's several similar posts out there, but I cannot find any with a solution to this issue. I want to add a (sort of) AudioLog when adding, changing or deleting entities (soft-delete) in Entity Framework 6. I've overridden the SaveChanges and because I only want to add log entries for EntityStates Added, Modified or Deleted, I fetch the list before I call SaveChanges the first time. The problem is, because I need to log what operation has been executed, I need to inspect the EntityState

How to get id from entity for Auditlog in Entity Framework 6

你离开我真会死。 提交于 2019-12-02 22:26:27
I know it's several similar posts out there, but I cannot find any with a solution to this issue. I want to add a (sort of) AudioLog when adding, changing or deleting entities (soft-delete) in Entity Framework 6. I've overridden the SaveChanges and because I only want to add log entries for EntityStates Added, Modified or Deleted, I fetch the list before I call SaveChanges the first time. The problem is, because I need to log what operation has been executed, I need to inspect the EntityState of the entities. But after SaveChanges is called, the EntityState is Unchanged for all entries. public

Django: form that updates X amount of models

泄露秘密 提交于 2019-12-02 10:23:50
问题 I have a page where it displays a filtered model instance list and allows users to update some fields of it or add new fields as a form. I am curious what wpuld be a clever way of doing this, to delete and resave all the input data or make comparison for each data and save edited / new fields& entities. I would like to mind you that I use postgres for saving these values and I display around 20 entries for this form. 回答1: The QuerySet object has the update() method - it's used in ie. Admin

Check if an insert or update was successful in Entity Framework

十年热恋 提交于 2019-12-01 17:36:07
In ADO.NET, ExecuteNonQuery() "For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command" ( http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx ) In EF v1, context.SaveChanges() method returns "The number of objects in an Added, Modified, or Deleted state when SaveChanges was called." ( http://msdn.microsoft.com/en-us/library/bb739065.aspx ) Please tell, when multiple entities (or single entity) are added or updated to context and context.SaveChanges() method is called, how to check if actual INSERT or

Entity Framework and MVC 3: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

雨燕双飞 提交于 2019-12-01 00:12:41
I have been trying to use one View for updating an object and all its child collections (based on one-to-many relationships in an SQL Server database with an Entity Framework model). It was suggested I should use AutoMapper, and I tried that and got it to work. (see Trying to use AutoMapper for model with child collections, getting null error in Asp.Net MVC 3 ). But the solution is really hard to maintain. And when I try the simple one I had to begin with, using an entity object directly as the model (a "Consultant" object, the parent of all the child collections), I am able to get all the

Entity Framework and MVC 3: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

天涯浪子 提交于 2019-11-30 18:04:26
问题 I have been trying to use one View for updating an object and all its child collections (based on one-to-many relationships in an SQL Server database with an Entity Framework model). It was suggested I should use AutoMapper, and I tried that and got it to work. (see Trying to use AutoMapper for model with child collections, getting null error in Asp.Net MVC 3 ). But the solution is really hard to maintain. And when I try the simple one I had to begin with, using an entity object directly as

Using TransactionScope with Entity Framework 6

非 Y 不嫁゛ 提交于 2019-11-30 11:57:49
问题 What I can't understand is if its possible to make changes to the context and get the changes in the same transaction before its commited. This is what I´m looking for: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (var context = new DbContext()) { //first I want to update an item in the context, not to the db Item thisItem = context.Items.First(); thisItem.Name = "Update name"; context.SaveChanges(); //Save change to this context //then I want to do a

EntityFramework show entities before saving changes

廉价感情. 提交于 2019-11-30 09:12:26
问题 Entity Framework ObjectSet with its method ToList shows just saved entities. That means, when I call context.AddToCustomers(myNewCust); and then (without calling SaveChanges) myDataGrid.DataContext = context.Customers.ToList(); the DataGrid doesn't show the newly added entity (even context.Customers.Count() doesn't include it). Is there any way to show these entities (those with EntityState = Added ) ? Thanks in advance. 回答1: I think you can get unsaved added entities by calling something

How can I cancel a user's WPF TreeView click?

时间秒杀一切 提交于 2019-11-30 06:35:24
问题 I've got a WPF application with a Treeview control. When the user clicks a node on the tree, other TextBox, ComboBox, etc. controls on the page are populated with appropriate values. The user can then make changes to those values and save his or her changes by clicking a Save button. However, if the user selects a different Treeview node without saving his or her changes, I want to display a warning and an opportunity to cancel that selection. MessageBox: Continue and discard your unsaved

Using TransactionScope with Entity Framework 6

五迷三道 提交于 2019-11-30 01:47:27
What I can't understand is if its possible to make changes to the context and get the changes in the same transaction before its commited. This is what I´m looking for: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (var context = new DbContext()) { //first I want to update an item in the context, not to the db Item thisItem = context.Items.First(); thisItem.Name = "Update name"; context.SaveChanges(); //Save change to this context //then I want to do a query on the updated item on the current context, not against the db Item thisUpdatedItem = context.Items