实体框架继承共享数据库字段

时间:2013-08-12 20:47:18

标签: entity-framework ef-code-first single-table-inheritance

所以,我正在使用Entity Framework Code First。这是模型:

public class StockMove
{
    public int Id { get; set; }

    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

public class StockMoveOut : StockMove
{
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }

    public int OriginLocalId { get; set; }
    public virtual Local OriginLocal { get; set; }
}

public class StockMoveIn : StockMove
{
    public int SupplierId { get; set; }
    public virtual Supplier Supplier { get; set; }

    public int DestinationLocalId { get; set; }
    public virtual Local DestinationLocal { get; set; }
}

public class StockMoveTransfer : StockMove
{
    public int OriginLocalId { get; set; } //should be the same for StockMoveOut
    public virtual Local OriginLocal { get; set; }

    public int DestinationLocalId { get; set; } //should be the same for StockMoveIn
    public virtual Local DestinationLocal { get; set; }
}

public class DataContext : DbContext
{
    public DbSet<StockMove> StockMoves { get; set; }
}

我需要引用StockMoveTransfer中的字段(OriginLocalId,DestinationLocalId)与StockMoveIn和StockMoveOut相同,但实体框架需要新字段。

生成的SQL DDL是:

create table [dbo].[StockMoves] (
    [Id] [int] not null identity,
    [ProductId] [int] not null,
    [CustomerId] [int] null,
    [OriginLocalId] [int] null,
    [SupplierId] [int] null,
    [DestinationLocalId] [int] null,
    [OriginLocalId1] [int] null, -- should not exists
    [DestinationLocalId1] [int] null, -- should not exists
    [Discriminator] [nvarchar](128) not null,
    primary key ([Id])
);

那么,我如何配置实体框架以指向现有字段?

0 个答案:

没有答案