For the sake of performance, I have decided to split a table following this technique. So basically I have a second entity in order to hold a binary field. These are my classes:
public partial class CustomerDoc
{
public byte[] Document { get; set; }
public int CustomerID { get; set; }
public virtual Customer customer { get; set; }
}
public partial class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public virtual CustomerDoc CustomerDoc { get; set; }
}
Now when I try to update the Document property in an existing Customer instance with the file uploaded, this value is not saved in the database, other properties are saved but not Document.
[HttpPost]
public virtual ActionResult Edit(Customer customer, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
//code to modify other properties
if (file != null && file.ContentLength > 0)
{
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes((int)file.InputStream.Length);
customer.CustomerDoc= new CustomerDoc { CustomerID = customer.CustomerID, Document = binData };
}
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
}
I've checked that other properties are modified correctly.
CustomerDoc have a value after SaveChanges call, but is not saved on database.
Also I've tried to update in a second instance of the same customer inside of the IF statement, but the I get a bunch of errors
This is the Mapping Detail:
Mapping Details - CustomerDoc
Maps to Customer
Column Mapping
CustomerID : int <-> *CustomerID : Int32
Document : varbinary(max) <-> Document: Binary