所以,我首先使用实体框架代码。这是模型:
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])
);
那么,如何配置实体框架以指向现有字段?