Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架在表拆分中保存一对一关系_C#_.net_Asp.net Mvc_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

C# 实体框架在表拆分中保存一对一关系

C# 实体框架在表拆分中保存一对一关系,c#,.net,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,为了表现,我决定分一张桌子如下。 所以基本上我有第二个实体来保存一个二进制字段。 这些是我的课程: 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 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; }
}
现在,当我尝试使用上载的文件更新现有客户实例中的Document属性时,该值不会保存在数据库中,其他属性也会保存,但不会保存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();
         }
我已检查其他属性是否正确修改

CustomerDoc在SaveChanges调用后有一个值,但不会保存在数据库中

此外,我还尝试在IF语句中更新同一客户的第二个实例,但是我得到了一系列错误

以下是映射详细信息:

Mapping Details - CustomerDoc
  Maps to Customer
    Column Mapping
       CustomerID : int <-> *CustomerID : Int32
       Document : varbinary(max) <-> Document: Binary 
映射详细信息-CustomerDoc 映射到客户 列映射 CustomerID:int*CustomerID:Int32 文档:varbinary(max)文档:Binary
将代码中的内部if块替换为以下内容:

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);
}
if(file!=null&&file.ContentLength>0)
{
BinaryReader b=新的BinaryReader(file.InputStream);
byte[]binData=b.ReadBytes((int)file.InputStream.Length);
var customerDoc=new customerDoc{CustomerID=customer.CustomerID,Document=binData};
db.Set().Add(customerDoc);
}

解决方案是独立于客户保存customerDoc实体

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();
}

你有适当的交往吗?您可以发布您的模型配置吗?CustomerDoc主键中是否有CustomerID?是的,它标记为实体键抛出此错误:“遇到无效数据。缺少必需的关系。请检查StateEntries以确定约束冲突的来源。”这可能有助于: