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

How do I find out the Windows 10 build number in Wix? [duplicate]
How to pass dataGridView values to reportViewer?
what is the best way to wait for animation to end in untiy5
Unable to determine composite primary key ordering for type
How to Change existing Imagelist image size of Listview
How to split 3DES key and calculate KCV in C#?
How to display list items on console window in C#?
Encrypt text using Caesar cipher C# [duplicate]
Proxy GZIP Compression
Getting SQL to work from C# with Dates
Store Enum as Char with Dapper
Drag and Drop Windows Forms: Controls dissapear after dragging
What is the best way to count all instances of a single process running under a particular user logon?
C# Security Protocol Specification
Ajax Dropdownlist using cascading dropdownlist in asp.net
Adding textbox value with button in C#

Categories

HOME
drupal
excel-formula
indexing
mysqli
microcontroller
phantomjs
opengl-es
perl6
flowtype
python-3.4
qt4
android-webview
token
theory
decoding
desktop-application
drush
eclipse-orion
popup
game-engine
wallet
loader
sha
knapsack-problem
ballerinalang
utc
large-files
typeorm
akamai
wordpress-rest-api
ibm-bpm
log4net-configuration
wercker
visual-prolog
scene7
eclipse-jdt
voyager
openvms
ansible-inventory
repeat
lighting
form-fields
const
framebuffer
compiled-query
uitextview
web-technologies
text-extraction
servicebus
taleo
ocamlbuild
jenkins-2
ionic2-select
standard-error
ear
e4
sharpdevelop
gige-sdk
webmatrix
rmq
acceleo
distcc
scikit-image
settext
netlink
jigsaw
kik
shtml
jde
nugetgallery
salesforce-service-cloud
windows-kernel
taverna
transport
notice
pushwoosh
glog
veeam
firebase-tools
numerical-recipes
backtrace
fxmlloader
os.walk
javacompiler
oxygenxml
banana-pi
flask-mongoengine
autofilter
angularjs-select2
qgraphicsscene
usersettings
symbian3
servicemanager
invalidoperationexception
linear-interpolation
netbiscuits
socketasynceventargs
code-organization
cherokee
chromebug
xslt-grouping
focus-stealing
high-load

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