I have a similar question to the one here:
OData update controller throwing 415 Unsupported media type error.
I have an OData Web API controller that has a put request to update a Product but so that I don't expose my database entity I would like the put method to accept a ProductDTO instead. Is this possible?
At the moment, I have the following in my ProductsController:
public IHttpActionResult Put([FromODataUri] int key, ProductDTO product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != product.ID)
{
return BadRequest();
}
Product DBProduct = AsProduct(product);
db.Entry(DBProduct).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(DBProduct);
}
And this is my WebApiConfig.cs file:
builder.EntitySet<Product>("Products");
builder.EntitySet<ProductDTO>("ProductDTO");
Where Product is:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public DateTime Timestamp { get; set; }
}
And ProductDTO:
public class ProductDTO
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
But I get the following message:
The entity type 'ProductService.DTOs.ProductDTO' is not compatible with the base type 'ProductService.Models.Product' of the provided entity set 'Container.Products'. When an entity type is specified for an OData feed or entry reader, it has to be the same or a subtype of the base type of the specified entity set.
There must be a way of doing this...any ideas?
Cheers