Class Properties Check

S

shapper

Hello,

I am updating an Entity as follows:

public void Update(DocumentUI updated) {

Document original = _context.Documents.FirstOrDefault(d =>
d.Id = updated.Id);
original.Title = updated.Title;
original.Published = updated.Published;
original.File = updated.File;
original.CreatedAt = updated.CreatedAt;
_context.SaveChanges();
}

So I have properties of type String, Int32, DateTime, Boolean and Byte
[].

The problem is that object DocumentUI updated contains only values of
the properties that were changed and not the others ...

How can I solve this? Should I make all properties on DocumentUI
nullable and then check for null before doing the map?

Thank You,
Miguel
 
P

Peter Duniho

Hello,

I am updating an Entity as follows:

public void Update(DocumentUI updated) {

Document original = _context.Documents.FirstOrDefault(d =>
d.Id = updated.Id);
original.Title = updated.Title;
original.Published = updated.Published;
original.File = updated.File;
original.CreatedAt = updated.CreatedAt;
_context.SaveChanges();
}

So I have properties of type String, Int32, DateTime, Boolean and Byte
[].

The problem is that object DocumentUI updated contains only values of
the properties that were changed and not the others ...

How can I solve this? Should I make all properties on DocumentUI
nullable and then check for null before doing the map?

You could. Or you could initialize all of the properties in the "updated"
object to the current values. Or you could track which ones changed some
other way. It's really up to you and what you think will work best in
your own code.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top