Class Properties Check

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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
 
Back
Top