So basically I'm trying to do this sequence of events in entity framework.
Create a new account
Get an existing account
Move all the data from the old account to the new account (transactions, users, etc)
Delete the old account
I'm doing this all in 'one go', inside a single ObjectContext.
It fails when I call SaveChanges on the context. I get an foreign key constraint error.
I checked this in SQL profiler and it turns out that entity framework isn't sending any of the updates, just the selected and then a delete.
I kinda understand WHY it is working like that but there must be some way to force it do work properly, without having to call SaveChanges() twice or something.
Hopefully.
My merge function basically looks like this
public void Merge(Account newAccount, Account oldAccount)
{
// ...
foreach (var user in oldAccount.Users.ToList())
{
oldAccount.Users.Remove(user);
newAccount.Users.Add(user);
}
// ...
_unitOfWork.Accounts.Delete(oldAccount);
}
The objects are POCO objects created by the E.F.4 POCO Entity Generator. To avoid pasting the entire class here's just one of the association properties with it's 'fixup' function.
public virtual ICollection<User> Users
{
get
{
if (_users == null)
{
var newCollection = new FixupCollection<User>();
newCollection.CollectionChanged += FixupUsers;
_users = newCollection;
}
return _users;
}
set
{
if (!ReferenceEquals(_users, value))
{
var previousValue = _users as FixupCollection<User>;
if (previousValue != null)
{
previousValue.CollectionChanged -= FixupUsers;
}
_users = value;
var newValue = value as FixupCollection<User>;
if (newValue != null)
{
newValue.CollectionChanged += FixupUsers;
}
}
}
}
private void FixupUsers(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (User item in e.NewItems)
{
item.Account = this;
}
}
if (e.OldItems != null)
{
foreach (User item in e.OldItems)
{
if (ReferenceEquals(item.Account, this))
{
item.Account = null;
}
}
}
}
You use object as reference to add and remove inside the for loop , the best solution to get object by key instate of using an object when add.
oldAccount.Users.Remove(user);
newAccount.Users.Add(users.FirstOrDefault(t=>t.ID = user.Id));
Okay figured out the solution. #CodeGorilla's comment gave me a hint.
Essentially I just need to call
_context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
before trying to delete the account. This forces entity framework to do all the updates in the database. After that there is no problem with deleting it.
Related
I tried this code to copy values from another object a deep clone but it doesnt seem to like nullable properties and I cannot figure out why
public static TConvert ConvertTo<TConvert>(this object entity) where TConvert : new()
{
var convertProperties = TypeDescriptor.GetProperties(typeof(TConvert)).Cast<PropertyDescriptor>();
var entityProperties = TypeDescriptor.GetProperties(entity).Cast<PropertyDescriptor>();
var convert = new TConvert();
foreach (var entityProperty in entityProperties)
{
var property = entityProperty;
var convertProperty = convertProperties.FirstOrDefault(prop => prop.Name == property.Name);
if (convertProperty != null)
{
convertProperty.SetValue(convert,
Convert.ChangeType(entityProperty.GetValue(entity),
convertProperty.PropertyType)); }
}
return convert;
}
I please a try catch around it and it brought me to this property which does exist in both models.
public int? PullUpHolds { get; set; }
How would I modify the above to take that into account I tried removing the if statment but that still caused a exception on the clone.
My Usuage is
private async void btnEndSession_Clicked(object sender, EventArgs e)
{
var item = dgWeightLifting.SelectedItem as WeightLifting;
if(item != null)
{
var removePlayer= await DisplayAlert(Constants.AppName,
$"This will remove the player {item.Players.FullName}
from the weight lifting screen. We will make a final
record for this session in work out history proceed",
"OK", "Cancel");
if (removePlayer)
{
var test =item.ConvertTo<Workout>();
}
}
}
I have an entity which is not connected to my dbcontext. I want to change that. However there is already another instance of the same entity attached to dbcontext. If I just add my new entity, I get an error, that an entity with the same primary key is already attached.
I tried multiple different variants of removing the old entity from dbcontext without any success. How can I replace the old instance with the new one?
Note: I don't want to copy the values, I want to attach this very instance of my new entity to dbcontext.
var entity = new MyEntity { Id = 1 };
var logicalDuplicate = dbcontext.Set<MyEntity >().Local
.FirstOrDefault(e => e.Equals(entity));
if (logicalDuplicate != null)
{
// remove logicalDuplicate from dbcontext
}
dbcontext.MyEntity.Attach(entity);
For clarification: I have overridden Equals to check for Id instead of reference.
Try this:
if (logicalDuplicate != null)
{
dbcontext.Entry(logicalDuplicate).State = EntityState.Detached;
dbcontext.MyEntity.Attach(entity);
dbcontext.Entry(entity).State = EntityState.Modified;
}
else
{
dbcontext.MyEntity.Add(entity);
}
How to get related entries
I investigated that and want to share with my results.
I used reflection as short way to get entity properties names. But it's possible to get it without reflection as mentioned #Florian Haider. You can use
answer and this.
// Found loaded related entries that can be detached later.
private HashSet<DbEntityEntry> relatedEntries;
private DbContext context;
private List<string> GetPropertiesNames(object classObject)
{
// TODO Use cache for that.
// From question https://stackoverflow.com/questions/5851274/how-to-get-all-names-of-properties-in-an-entity
var properties = classObject.GetType().GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
return properties.Select(t => t.Name).ToList();
}
private void GetRelatedEntriesStart(DbEntityEntry startEntry)
{
relatedEntries = new HashSet<DbEntityEntry>();
// To not process start entry twice.
relatedEntries.Add(startEntry);
GetRelatedEntries(startEntry);
}
private void GetRelatedEntries(DbEntityEntry entry)
{
IEnumerable<string> propertyNames = GetPropertiesNames(entry.Entity);
foreach (string propertyName in propertyNames)
{
DbMemberEntry dbMemberEntry = entry.Member(propertyName);
DbReferenceEntry dbReferenceEntry = dbMemberEntry as DbReferenceEntry;
if (dbReferenceEntry != null)
{
if (!dbReferenceEntry.IsLoaded)
{
continue;
}
DbEntityEntry refEntry = context.Entry(dbReferenceEntry.CurrentValue);
CheckReferenceEntry(refEntry);
}
else
{
DbCollectionEntry dbCollectionEntry = dbMemberEntry as DbCollectionEntry;
if (dbCollectionEntry != null && dbCollectionEntry.IsLoaded)
{
foreach (object entity in (ICollection)dbCollectionEntry.CurrentValue)
{
DbEntityEntry refEntry = context.Entry(entity);
CheckReferenceEntry(refEntry);
}
}
}
}
}
private void CheckReferenceEntry(DbEntityEntry refEntry)
{
// Add refEntry.State check here for your need.
if (!relatedEntries.Contains(refEntry))
{
relatedEntries.Add(refEntry);
GetRelatedEntries(refEntry);
}
}
Edit This finds the original product, removes it, and adds the new one:
static void UpdateDatabase()
{
Context context = new Context();
Product product1 = context.Products.Find(1);
context.Products.Remove(product1);
Product product2 = new Product(){ProductId = 1, Name = "Product2"};
context.Products.Add(product2);
context.SaveChanges();
}
Best way to salve this problem is
db is my database Object
updateprice is my database entity object
ep is my old same database entity object
db.Entry(updateprice).CurrentValues.SetValues(ep);
I have a web form with around 50 fields that is used for crud operations on an Oracle DB, I am using EF6.
Currently, I accomplish this like so:
private GENERIC_FTP_SEND GetFields()
{
GENERIC_FTP_SEND ftpPartner = new GENERIC_FTP_SEND();
//Contact Info
ftpPartner.FTP_LOOKUP_ID = FTP_LOOKUP_IDTB.Text;
ftpPartner.PARTNER_NAME = PARTNER_NAMETB.Text;
ftpPartner.REMEDY_QUEUE = REMEDY_QUEUETB.Text;
ftpPartner.PRIORITY = PRIORITYBtns.SelectedValue;
ftpPartner.CONTACT_EMAIL = CONTACT_EMAILTB.Text;
ftpPartner.CONTACT_NAME = CONTACT_NAMETB.Text;
ftpPartner.CONTACT_PHONE = CONTACT_PHONETB.Text;
...
}
where GENERIC_FTP_SEND is the name of the virtual DbSet in my Model.context.cs.
This works fine but is not reusable in the least. What I would like to accomplish is to have some code that allows me to iterate through the attributes of ftpPartner and compare them to the field id for a match. Something like this:
var n =0;
foreach (Control cntrl in ControlList){
if(cntrl.ID == ftpPartner[n]){
ftpPartner[n] = cntrl.Text;
}
n++;
}
In case you need/want to see it here is my Model.context.cs
public partial class Entities : DbContext{
public Entities(): base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<GENERIC_FTP_SEND> GENERIC_FTP_SEND { get; set; }
}
I saw the question here but I am not sure how to implement that in my case.
Entity Framework 6: is there a way to iterate through a table without holding each row in memory
You can achieve that with reflection:
var type = typeof(GENERIC_FTP_SEND);
foreach (Control cntrl in ControlList){
Object value = null;
if (cntrl is TextBox){
value = (cntrl as TextBox).Text;
} else if (cntrl is GroupBox){
value = (cntrl as GroupBox).SelectedValue;
} //etc ...
PropertyInfo pInfo = type.GetProperty(cntrl.ID);
if (pInfo != null && value != null){
pInfo.SetValue(ftpPartner, value, null);
}
}
You can also use the Entity Framework context object as well to accomplish the same if you know you are going to insert.
var x = new GENERIC_FTP_SEND();
// Add it to your context immediately
ctx.GENERIC_FTP_SEND.Add(x);
// Then something along these lines
foreach (Control cntrl in ControlList)
{
ctx.Entry(x).Property(cntrl.Name).CurrentValue = ctrl.Text;
}
I am trying to prevent any deletes on my database tables. Currently using Entity Framework 5. Firstly here is my code,
public override int SaveChanges()
{
var Changed = ChangeTracker.Entries();
if (Changed != null)
{
foreach (var entry in Changed.Where(e => e.State == EntityState.Deleted))
{
entry.State = EntityState.Unchanged;
}
}
return base.SaveChanges();
}
I've managed to prevent it with this way. When i use Remove method of EF its not working anymore.However, what i am trying to achieve is, when i use the remove method for the given ID, i want to set isDeleted(which is a (bit) column in all my db tables) value to false. Currently, i am lost in the documents and shared codes around the internet.
Thanks
I would probably handle this by making entities that are soft deletable implement an interface, something like ISoftDeletable.
public interface ISoftDeletable
{
bool IsDeleted { get; set; }
}
Then extend your code above to check if the entity type implements the ISoftDeletable interface, if it does simply set IsDeleted to true.
public override int SaveChanges()
{
var Changed = ChangeTracker.Entries();
if (Changed != null)
{
foreach (var entry in Changed.Where(e => e.State == EntityState.Deleted))
{
entry.State = EntityState.Unchanged;
if (entry.Entity is ISoftDeletable)
{
// Set IsDeleted....
}
}
}
return base.SaveChanges();
}
You would then need to make sure queries for the Entities that implement ISoftDeletable filter out those that are soft deleted.
Building on #BenjaminPauls great answer, but using the generic Entries<TEntity>. In my opinion it cleans up the code and the nestling a bit.
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries<ISoftDeletable>())
{
if (entry.State == EntityState.Deleted)
{
// Set deleted.
}
}
return base.SaveChanges();
}
Or even:
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries<ISoftDeletable>()
.Where(x => x.State == EntityState.Deleted)
{
// Set deleted.
}
return base.SaveChanges();
}
I have two tables one called Companies and the other called Locations. Companies has a Id and Name, Locations has Id, CompanyId, Name, and SubAccount. I have two projects. One as IMS.Data where all the validation is and IMS where the webforms pages are. I am having trouble validating that if the company has a location(if the company id is a foreign key anywhere) then do not delete the record. Here is what I have so far and everything works but I can not reference the Locations CompanyId in order to do a check using lambda expressions. can anyone help me I am new to lambda expressions.
Here is the method I am using for the validation
namespace IMS.Data
{
public class CompanyContext : IMSDBContext
{
public Company DeleteCompany(Company company)
{
if (company.Name == null)
{
throw new Exception("Please select a record to delete.");
}
if (Companies.Any(x => x.Name == company.Name))
{
throw new Exception("Can not delete a company that has a location.");
}
Companies.Remove(company);
return company;
}
}
}
Here is the the delete button I use
namespace IMS
{
public partial class CompanySetUp : Page
{
private const string AddButton = "Add";
private const string SaveButton = "Save";
private const string DeleteButton = "Delete";
private const string CancelButton = "Cancel";
private int CompanyId // This puts the "CompanyId" into a viewstate and is used to update the record
{
get
{
return (int)ViewState["_companyId"];
}
set
{
ViewState["_companyId"] = value;
}
}
private IList<Company> Companies { get; set; } // This gets and sets the list of companies from the table "Companies"
protected void Page_Load(object sender, EventArgs e)
{
PopulateCompanyListGrid();
//if (Companies != null && Companies.Count > 0) // This will put a record in the "txtCompanyName.Text" on page load
//{
// txtCompanyName.Text = Companies.First().Name;
//}
}
protected void btnDelete_Click(object sender, EventArgs e) // This will delete the record that matches the textbox or throw an exception
{
CompanyContext context = null;
switch (btnDelete.Text)
{
case DeleteButton:
try
{
context = new CompanyContext();
var company = context.Companies.ToList().First(x => x.Name == txtCompanyName.Text);
context.DeleteCompany(company);
//PopulateCompanyListGrid();
Reload();
}
catch (Exception ex)
{
lblCompanyNameNotification.Text = ex.Message;
}
finally
{
if (context != null)
{
context.Dispose();
}
}
PopulateCompanyListGrid();
break;
case CancelButton:
Reload();
break;
}
}
If you have a relational database with this data and foreign keys set up properly, you could commit the change and watch for an SqlException with code 547, which is a foreign-key exception. This is thrown when the data to delete is referenced by other tables.
The advantage of handling it this way around is that the data store enforces itself to be valid, instead of defining checks for all the foreign key relations in your code. If you add new FK's later, they will automaticly be enforced by the database and caught by your code, rather than you having to add new checks to the code itself.