当使用实体框架code第一映射属性设置为单独的表,移外键字段 [英] When using entity framework code-first mapping property to separate table, moves foreign key field

查看:161
本文介绍了当使用实体框架code第一映射属性设置为单独的表,移外键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有奇怪的问题与code-第一EF映射表。我有一个父类合同有许多-1从其它类供应商的关系。要求上来来存储合同主体在合同的扫描件。为了保持不必查询文件字节每一次,我想这个属性存储在一个单独的表。我可以使EF存储ContractDocument在一个单独的表,但由于某些原因,FK向供应商终止于ContractDocument表而不是父表

Having odd problem with mapping tables with code-first EF. I have a parent class "Contract" with a many-1 relationship from another class "Supplier". The requirement came up to store a scanned copy of the contract within the Contract entity. To keep from having to query the document bytes every time, I want to store this property in a separate table. I can make EF store the ContractDocument in a separate table, but for some reason the FK to suppliers ends up in the ContractDocument table instead of the parent table.

public class Contract
{
    public Guid Id {get;set;}
    ...
    public virtual ContractDocument Document {get;set;}
    public virtual Supplier Supplier {get;set;}
}

public class ContractDocument
{
    public string MimeType {get;set;}
    public byte[] Data {get;set;}
}

在的DbContext:

In DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contract>()
                .Map(m => m.Properties(p => p.Document))
                .ToTable("ContractDocument");
    base.OnModelCreating(modelBuilder);
}

表ContractDocument用正确的属性创建,并保存数据。然而,它也包含了FK字段Supplier_Id。 合同表中不再包含任何FK领域。

The table "ContractDocument" is created with correct properties, and does save data. However, it also contains the FK field "Supplier_Id". The "Contract" table no longer contains any FK fields.

推荐答案

我不能肯定,但没有VS测试的那一刻,但我认为,当我们把一个实体到多个表,你需要映射所有的领域,而不仅仅是那些有不寻常。

I'm not certain and don't have VS to test at the moment, but I think when you split an entity into multiple tables, you need to map all the fields, not just the ones that are "unusual".

尝试沿着这些线路更新你的映射的东西...

Try updating your mapping to something along these lines...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contract>()
                .Map(m => {
                       m.Properties(p => 
                           new {
                              p.Id,
                              p.Document
                           });
                       m.ToTable("ContractDocument");
                    })
                .Map(m => {
                       m.Properties(p =>
                           new {
                              p.Id,
                              /* everything else NOT document */
                           });
                       m.ToTable("Contract");
                    });
    base.OnModelCreating(modelBuilder);
}

固定的基础上

修改 根据您的评论,这听起来像你需要定义的关系,而不是实体的分裂。你有单独的表和单独的类,但它们之间的关系是怎样的缺失。也许尝试这样的事情。

Edit based on your comment, it sounds like you need to define the relationship, not entity splitting. You have separate tables and separate classes, but the relationship between them is what is missing. Maybe try something like this.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contract>().ToTable("Contract");
    modelBuilder.Entity<ContractDocument>().ToTable("ContractDocument");
    modelBuilder.Entity<Contract>()
        .HasOptional(c => c.Document)
        .WithRequiredDependent();

第二次修改 好吧,这样做是一个复杂的类型,我认为你需要的第一个实体分割图的东西加了 modelBuilder.ComplexType&LT; ContractDocument&GT;();

2nd Edit Okay, to do it as a complex type, I think you need the first entity splitting map stuff plus a modelBuilder.ComplexType<ContractDocument>(); line.

这篇关于当使用实体框架code第一映射属性设置为单独的表,移外键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆