c#


InvalidOperationException in a custom EF Core save method


I am writing a custom save generic method for our framework in EF Core. The idea is to have a method that can make partial updates.
The code is like this:
public IEnumerable<T> UpdateEntitiesByProperties(IEnumerable<UpdateByPropertiesData<T>> entitiesAndProperties)
{
List<T> entities = new List<T>();
foreach (var entityAndProperties in entitiesAndProperties)
{
T contextEntity = this.context.Find<T>(entityAndProperties.Entity.Id);
EntityEntry<T> changeTrackerEntityEntry = this.context.Entry(contextEntity);
changeTrackerEntityEntry.CurrentValues.SetValues(entityAndProperties.Entity);
foreach (var property in changeTrackerEntityEntry.Properties.Where(p => !p.Metadata.IsConcurrencyToken))
{
property.IsModified = entityAndProperties.UpdatedProperties.Contains(property.Metadata.Name);
}
entities.Add(contextEntity);
}
return entities;
}
If I change the code to this, it works correctly:
public IEnumerable<T> UpdateEntitiesByProperties(IEnumerable<UpdateByPropertiesData<T>> entitiesAndProperties)
{
List<T> entities = new List<T>();
foreach (var entityAndProperties in entitiesAndProperties)
{
T contextEntity = this.context.Find<T>(entityAndProperties.Entity.Id);
EntityEntry<T> changeTrackerEntityEntry = this.context.Entry(contextEntity);
changeTrackerEntityEntry.CurrentValues.SetValues(entityAndProperties.Entity);
foreach (var property in changeTrackerEntityEntry.Properties.Where(p => !p.Metadata.IsConcurrencyToken))
{
if(entityAndProperties.UpdatedProperties.Contains(property.Metadata.Name)
{
property.CurrentValue = typeof(T).GetTypeInfo().GetDeclaredProperty(property.Metadata.Name).GetValue(entityAndProperties.Entity);
}
}
entities.Add(contextEntity);
}
return entities;
}
The method is working with simple entities but throws an InvalidOperationException when the entity in entitiesAndProperties does not contain a required field.
The exception text is:
The property 'Name' on entity type 'User' is marked as null, but this cannot be saved because the property is marked as required.
It should not be a problem, because the entity already exists in database with the required field value, contextEntity reflects that, but seems the CurrentValues.SetValues overrides this.

Related Links

c# vs2012 rdlc, missing report parameters
Regular expression for for one or more white spaces, tab, newline
What is the best number of threads for my task? Task or ThreadPool?
Avro .Net Serializer ignores attributes
DropDownList is causing an invalid postback or callback argument.but which one?
Copying a byte array to a bitmap using unsafe block
Convert Julian Date with Time (H/m/s) to Date Time in C#
TcpClient connecting to unavailable service
How do can I programmatically automate the comparison of Visual Studio Solution Files?
Code doesn't execute, while storing data's to ms-access database from c#? [duplicate]
change textblock text that is inside Listbox in windowsphone 8
Runtime exception thrown while using Pack URI
inheritance and passing parameters
C# Loop through DataGridView - MySqlDataReader stops after 2 Rows?
C# Form Region - Capture the Outlook 2013 Send event
Session Key with spaces ASP.NET

Categories

HOME
autohotkey
customization
mysqli
data-modeling
servicenow
qt4
pip
samsung-gear-s2
ionic-native
cracking
soap-client
osclass
tmux
element
lambda-calculus
css-float
reportlab
cucumberjs
caldav
gsuite
workflow-foundation
uibarbuttonitem
osgi-bundle
pushpad
mobile-development
deferred
edsdk
wordpress-rest-api
ng2-bootstrap
directions
sampling
ansible-inventory
maven-2
latency
ngrok
linkurious
mediastream
idea-plugin
keychain
mixed-models
delphi-xe
electron-packager
datagridviewcombobox
amazon-rds-aurora
python-venv
spring-data-commons
geminabox
history
critical-section
breadcrumbs
lattice
joystick
key-value-store
bytearray
audiorecord
boost-propertytree
jcalendar
code-translation
google-app-engine-php
computability
gtkwave
wiql
personality-insights
traminer
textblob
eula
sicstus-prolog
xmla
sony-xperia
esri-arc-engine
conditional-comments
register-allocation
yosemite
im4java
angular-file-upload
git-subtree
facebook-java-api
android-jack-and-jill
complete
grunt-wiredep
thucydides
oc4j
violation
jython-2.5
django-tinymce
ncml
mail-gem
webpage-screenshot
datasheet
inserthtml
jexcelapi
meego-harmattan
arraycollection
chars
browser-state

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App