4

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 
Sjon
  • 4,989
  • 6
  • 28
  • 46
Roberto
  • 55
  • 2
  • 9

2 Answers2

0

Replace the inner if block in your code with the following:

if (file != null && file.ContentLength > 0)
{
    BinaryReader b = new BinaryReader(file.InputStream);
    byte[] binData = b.ReadBytes((int)file.InputStream.Length);

    var customerDoc = new CustomerDoc { CustomerID = customer.CustomerID, Document = binData };
    db.Set<CustomerDoc>().Add(customerDoc);
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • Throw this error: "Invalid data encountered. A required relationship is missing. Examine StateEntries to determine the source of the constraint violation." – Roberto Aug 05 '13 at 15:47
  • 1
    Maybe this helps: http://stackoverflow.com/questions/8170975/entity-framework-split-table-delete – ataravati Aug 07 '13 at 18:23
0

Solution is to save the customerDoc entity independently from customer.

if (file != null && file.ContentLength > 0)
{
    BinaryReader b = new BinaryReader(file.InputStream);
    byte[] binData = b.ReadBytes((int)file.InputStream.Length);

    var customerDoc = new CustomerDoc { CustomerID = customer.CustomerID, Document = binData };
    db.Entry(customerDoc).State = EntityState.Modified;
    db.SaveChanges();
}
Roberto
  • 55
  • 2
  • 9