Im trying to give a list of propertynames to my entity framework update method and these propertynames should be marked as unchanged. However It does get the properties, but than it gives the following error when trying to change the state:
The entity type RuntimePropertyInfo is not part of the model for the current context.
Which makes sense but Is there a way to get the object property that is on the current context?
Here is my code:
if (propertiesToSkip != null)
{
foreach (var propertyName in propertiesToSkip)
{
var prop = entity.GetType().GetProperty(propertyName);
_context.Entry(prop).State = EntityState.Unchanged;
}
}
I hope anybody can help.
Edit
public static void MigrateIqrAccountDetails(ref IUnitOfWork uow, Account account,
IEnumerable<List<string>> iqrContacts)
{
if (account.IqrAccount == null) throw new ArgumentException("Cannot migrate Account");
// Addresses
var addresses = ContactMigrationHelper.MigrateIqrContactAddresses(contact);
if (addresses != null)
account.Addresses.AddRange(addresses.Except(account.Addresses, new Comparers.AddressComparer()));
uow.Repository<IqrAccount>().Update(account.IqrAccount);
uow.Repository<Account>().Update(account, new List<string> {"Name"});
}
Here is the method that edits the object and should update it on the context.
The account is being gathered like this:
var accountToUpdate =
uow.Repository<Account>()
.GetAll(a => a.IqrAccount != null, "PhoneNumbers", "EmailAddresses", "Addresses")
.OrderBy(a => a.IqrAccount.UpdatedAt)
.FirstOrDefault();
In the repository that looks like this:
public IEnumerable<T> GetAll(Func<T, bool> predicate = null, params string[] includes)
{
var query = Include(_objectSet, includes);
return predicate != null ? query.Where(predicate).Where(o => !o.IsRemoved) : query.Where(o => !o.IsRemoved);
}
protected virtual IQueryable<T> Include(IQueryable<T> query, params string[] includes)
{
if (includes == null) return query;
query = includes.Aggregate(query, (current, item) => current.Include(item));
return query;
}
and the error im getting is:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Addresses_dbo.Accounts_AccountId". The conflict occurred in database "Test", table "dbo.Accounts", column 'Id'. The statement has been terminated.
When savechanges is called.
Also here are my models:
public class Account : BaseEntity
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual List<Address> Addresses { get; set; }
public int? CommentCollectionId { get; set; }
public virtual LabelCollection LabelCollection { get; set; }
public virtual User AccountManager { get; set; }
[ForeignKey("ParentAccount")]
public virtual int? ParentAccountId { get; set; }
public virtual Account ParentAccount { get; set; }
public virtual ICollection<Account> SubAccounts { get; set; }
}
public class Address : BaseEntity
{
public int Id { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public string NumberExtension { get; set; }
public string ZipCode { get; set; }
public bool IsActive { get; set; }
public string Premise { get; set; }
public string SubPremise { get; set; }
public Location Coordinates { get; set; }
[ForeignKey("Account")]
public int AccountId { get; set; }
public Account Account { get; set; }
}
I guesse the problem is that the account is passed as a seperate object parameter and so it is no longer on the context? What is the good way of doing this?