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

frostDays doesn't exist
store retrieve IObservable subscription state in Rx
Merge two lists
Add SSRS project created in BIDS to MVC3 project
Foreign Key Just showing a number which is associated with the primary key
C# XNA movement not working, can't figure why
Convert mp4 to mp3 [closed]
opening a document and waiting for a user to finish editing it
Why is there no “date” shorthand of System.DateTime in C#?
Entity Framework - Why Does This Work?
Create a NHibernate Dialect that adds with(nolock) to every SELECT [duplicate]
I do not have a 'Any CPU' option present in my Configuration Manager
WCF Data Services, Entity Framework, LINQ -> Children within children
How do I determine the sender of an email via Exchange Web Services in C#?
c# fast sort and retrieve for small amount of key value pairs
MySQL Connector - removes polish characters

Categories

HOME
oauth-2.0
actionscript-3
http
oop
jenkins-pipeline
process
glsl
where
itext7
salesforce-lightning
squeak
kendo-asp.net-mvc
restheart
fetch
aspectj
jetbrains
mongodb-replica-set
javascript-debugger
web-testing
web-worker
pylint
amazon-data-pipeline
android-actionbar
django-rest-auth
project-structure
fibonacci
rndis
javaagents
procedural-generation
datastax-enterprise
oracle10g
traveling-salesman
bytebuffer
csproj
edirectory
ios-autolayout
epub
rtos
sampling
kofax
hippocms
mybatis-generator
ninja-forms
ui-grid
fisheye
icu
pyexcel
quartz
gradlew
togglebutton
jenkins-2
monaca
cloudconvert
jcs
cancan
visio-2010
dotnet-cli
dynamic-linking
liferay-ide
nsalert
chain
codesniffer
disconnect
laterjs
jde
boost-propertytree
radar-chart
factorization
instabug
event-tracking
vensim
playn
firewire
web-standards
nfa
connection-timeout
pymol
chrome-mobile
cl.exe
conkeror
yosemite
selenium-grid2
pytz
winrun4j
android-jack-and-jill
php-amqplib
nop
myrrix
removeall
urlfetch
gpl
motordriver
beep
diazo
nsundomanager
virtual-printer
parallel-python
windows-xp-embedded
noise-reduction
chars
text-manipulation
iphone-maps
helios
mdd
dojox.gfx
scala-designer
projectgen
file-icons

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