I'm using Code-First entity framework with Web API. I need to send from fiddler a test model object against a post method in a controller. The model has these parameters:
[Key]
public int ID { get; set; }
public string Caption { get; set; }
[JsonIgnore]
public byte[] Image { get; set; }
[NotMapped]
public string ImageDataString { get; set; }
the ImageDataString is the string representation of my Image. It shouldn't be resulted into a column in the DB; therefore, it is marked with NotMapped. Its purpose is ended when I do the conversion:
protected override UserImage CreateEntity(UserImage entity)
{
entity.Image = Convert.FromBase64String(entity.ImageDataString);
because it is easier to pass on from the client a string instead of a byte[].
Now the goal is to be able to simulate sending the entity object from fiddler. because the property is not mapped, the entity is showing up null. If I remove the [NotMapped] tag, the entity object will be populated fine.
here is my fiddler's body message in compose:
{ "Caption":"hello World", "ImageDataString":"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCkkR1N/DVrd6ikBgtNZtv9H1SxAWUJsnX5mVGGZZdSxtB0anyfZ9z7TgLjnH8J5" }
How can I combine the use of NotMapped to stop the creation of a column in DB but at the same time still be able to use it during a post? Is it inherently impossible with EF? What else do you suggest?
I suggest your entities only hold fields that are going into the database.
Create a separate view model to send to the view, in which you have the imagedatastring property. If you like you can even let it inherit the actual entity.