When using entity framework code-first mapping property to separate table, moves foreign key field

你离开我真会死。 提交于 2019-12-23 20:09:23

问题


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

In DbContext:

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

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.


回答1:


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

Fixing based on this

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

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.



来源:https://stackoverflow.com/questions/9434082/when-using-entity-framework-code-first-mapping-property-to-separate-table-moves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!