Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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/0/search/2.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
Entity framework 使用实体拆分时更新对象-先编码_Entity Framework - Fatal编程技术网

Entity framework 使用实体拆分时更新对象-先编码

Entity framework 使用实体拆分时更新对象-先编码,entity-framework,Entity Framework,我有一个名为Document的类,我将其拆分为两个实体,以分离一个昂贵的二进制字段: [Table("Document")] public class Document { [Key] public int Id { get; set; } ... other fields ... [Required] public virtual DocumentBinary DocumentBinary { get; set; } } [Table("Docum

我有一个名为Document的类,我将其拆分为两个实体,以分离一个昂贵的二进制字段:

[Table("Document")]
public class Document
{
    [Key]
    public int Id { get; set; }

    ... other fields ...

    [Required]
    public virtual DocumentBinary DocumentBinary { get; set; }
}

[Table("Document")]
public class DocumentBinary
{
    [Key, ForeignKey("Document")]
    public int DocumentId { get; set; }

    public Document Document { get; set; }

    public byte[] DocumentData { get; set; }
}
因此,一切正常,两个实体共享同一个数据库表,DocumentData仅在需要时加载

然而,当涉及到更新文档实体时,我得到一个错误,指出“DocumentBinary是必需的”

从DocumentBinary virtual property中删除[Required]属性时,出现以下错误:

实体类型“Document”和“DocumentBinary”无法共享表“Documents”,因为它们不在同一类型层次结构中,或者它们之间没有有效的一对一外键关系,并且主键匹配

我显然可以这样做:

var test = document.DocumentBinary;
在更新文档对象之前:

documentRepository.Update(document);

这将根据我的请求加载二进制数据并保存更改,而不会出现任何问题,但关键是我不需要这样做。

这可以通过使用fluent API实现。如果删除数据注释,并在
OnModelCreating
中添加该注释,则该注释应能正常工作

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     modelBuilder.Entity<Document>().HasRequired(d => d.DocumentBinary).
                                     WithRequiredDependent(db => db.Document);

 }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasRequired(d=>d.DocumentBinary)。
带RequiredDependent(db=>db.Document);
}

我通过覆盖DocumentRepository中的更新方法解决了此问题:

public override void Update(Document document)
{
    try
    {
        DataContext.Entry(document.DocumentBinary).State = EntityState.Modified; // added this line
        DataContext.Entry(document).State = EntityState.Modified;
    }
    catch (System.Exception exception)
    {
        throw new EntityException("Failed to update document");
    }
}

我知道它可能与我通过将DocumentBinary赋值给“test”变量来评估DocumentBinary的功能相同,但它看起来是一个更干净的解决方案。

谢谢。它可能会工作(以前尝试过,但顺序错误!),但我更喜欢使用数据注释,因为我发现它们更具描述性。我自己刚刚设法找到了一个解决方案,稍后我会发布。