实体框架 - 继承 - 零对一关系到子对象,如何映射? (流利的API) [英] Entity Framework - Inheritance - Zero to one Relationship to child object, how to map? (Fluent API)

查看:254
本文介绍了实体框架 - 继承 - 零对一关系到子对象,如何映射? (流利的API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承层次结构,其中Action是ActionCompleted和ActionCancelled的父项。 Order类具有零到一个ActionCompleted和ActionCancelled。我尝试过TPH和TPT(甚至尝试过edmx),但是无法让Entity了解Order和Child动作之间的关系,请建议如何映射?

  // Classes 
public class Order
{
public int OrderId {get;组; }
public string Name {get;组; }
public ActionCompleted ACO {get;组; }
public ActionCancelled ACA {get;组; }

}

public class Action
{
public int ActionID {get;组; }
public DateTime ActionDT {get;组; }
public Order Order {get;组; }
}

public class ActionCompleted:Action
{

}
public class ActionCancelled:Action
{
public int CancelledByPhysician {get;组;
}

// Mappings



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $




public DbSet< Order>订单{get;组; }

public DbSet< Action>行动{get;组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Order>()。ToTable(Orders);

modelBuilder.Entity< Action>()。HasKey(a => a.ActionID);

modelBuilder.Entity< Action>()。Map(m => m.ToTable(Actions))
.Map< ActionCompleted>(m => m.ToTable ActionCompleted))
.Map< ActionCancelled>(m => m.ToTable(ActionCancelled));

//modelBuilder.Entity<Action>().HasRequired(a => a.Order).WithOptional()。Map(m => m.MapKey(DiagOrderId));
modelBuilder.Entity< ActionCompleted>()。HasRequired(a => a.Order).WithOptional()。Map(m => m.MapKey(DiagOrderId));
modelBuilder.Entity< ActionCancelled>()。HasRequired(a => a.Order).WithOptional()。Map(m => m.MapKey(DiagOrderId));

}



}



用于保存数据的代码:

  EDISContext db = new EDISContext(); 
var ord1 = db.Orders.FirstOrDefault(o => o.OrderId == 1);
ActionCompleted ac = new ActionCompleted();
ac.ActionDT = DateTime.Now;
ac.Order = ord1;
db.Actions.Add(ac);
db.SaveChanges();

DB表脚本:

  CREATE TABLE [dbo]。[Orders](
[OrderId] [int] IDENTITY(1,1)NOT NULL,
[Name] [varchar](50) NULL,
)ON [PRIMARY]

CREATE TABLE [dbo]。[Actions](
[ActionID] [int] IDENTITY(1,1)NOT NULL,
[ActionDT] [datetime] NOT NULL,
[DiagOrderID] [int] NULL,
)ON [PRIMARY]

CREATE TABLE [dbo]。[ActionCompleted]
[ACID] [int] IDENTITY(1,1)NOT NULL,
[ActionID] [int] NOT NULL,
)ON [PRIMARY]

CREATE TABLE [dbo]。[ActionCancelled](
[ACAID] [int] IDENTITY(1,1)NOT NULL,
[ActionID] [int] NOT NULL,
[CancelledByPhysician] [int ] NULL,
)ON [PRIMARY]


解决方案

首先使用代码时,至少尝试让EF为您创建数据库总是有用的 - 这是默认行为(将查找称为SQL Server的本地实例.\SQLEXPRESS)。如果您手动创建数据库并想要控制列名称,那就是发生什么,您需要使用流畅的API指定列名。请参阅 http://msdn.microsoft.com/en -us / library / hh295845(v = vs103).aspx http://msdn.microsoft.com/en-us/library/hh295847(v = VS.103).aspx 的一些例子应该涵盖你被卡住了on。



仅供参考,您创建的数据库将具有以下表和列名称:

  CREATE TABLE [dbo]。[ActionCancelled](
[ActionID] [int] NOT NULL,
[CancelledByPhysician] [int] NOT NULL,
PRIMARY KEY CLUSTERED([ActionID] ASC)


CREATE TABLE [dbo]。[ActionCompleted](
[ActionID] [int] NOT NULL,
PRIMARY KEY CLUSTERED([ActionID] ASC)


CREATE TABLE [dbo]。[Actions](
[ActionID] [int] IDENTITY(1,1)NOT NULL,
[ActionDT] [datetime] NOT NULL,
[Order_Orde rId] [int] NULL,
PRIMARY KEY CLUSTERED([ActionID] ASC)


CREATE TABLE [dbo]。[Orders](
[OrderId] [int] IDENTITY(1,1)NOT NULL,
[Name] [nvarchar](max)NULL,
[ACO_ActionID] [int] NULL,
[ACA_ActionID] [int] NULL ,
PRIMARY KEY CLUSTERED([OrderId] ASC)


I have a Inheritance Hierarchy where Action is parent of ActionCompleted and ActionCancelled. Order class has a zero to one ActionCompleted and ActionCancelled. I have tried TPH and TPT (even tried edmx) but unable to get Entity to understand this relationship between Order and child actions, please suggest how do I map?

//Classes
    public class Order
    {
        public   int OrderId { get; set; }
        public string Name { get; set; }
        public   ActionCompleted ACO { get; set; }
        public ActionCancelled ACA { get; set; }

    }

public class Action
{
    public   int ActionID { get; set; }
    public   DateTime ActionDT { get; set; }
    public   Order Order { get; set; }
}

public class ActionCompleted : Action
{

}
public class ActionCancelled : Action
{
    public int CancelledByPhysician { get; set; }
}

//Mappings

public class EDISContext:DbContext
{
    public EDISContext()
        : base("EDISContext")
    { }


    public DbSet<Order> Orders { get; set; }

    public DbSet<Action> Actions { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Order>().ToTable("Orders");

        modelBuilder.Entity<Action>().HasKey(a => a.ActionID);

        modelBuilder.Entity<Action>().Map(m => m.ToTable("Actions"))
                                              .Map<ActionCompleted>(m => m.ToTable("ActionCompleted"))
                                              .Map<ActionCancelled>(m => m.ToTable("ActionCancelled"));

        //modelBuilder.Entity<Action>().HasRequired(a => a.Order).WithOptional().Map(m => m.MapKey("DiagOrderId"));
        modelBuilder.Entity<ActionCompleted>().HasRequired(a => a.Order).WithOptional().Map(m => m.MapKey("DiagOrderId"));
        modelBuilder.Entity<ActionCancelled>().HasRequired(a => a.Order).WithOptional().Map(m => m.MapKey("DiagOrderId"));

    }

}

Code used to save data:

EDISContext db = new EDISContext();
var ord1 = db.Orders.FirstOrDefault(o => o.OrderId == 1);
ActionCompleted ac= new ActionCompleted();
ac.ActionDT = DateTime.Now;
ac.Order = ord1;
db.Actions.Add(ac);
db.SaveChanges();

Script of DB tables:

CREATE TABLE [dbo].[Orders](
    [OrderId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].[Actions](
    [ActionID] [int] IDENTITY(1,1) NOT NULL,
    [ActionDT] [datetime] NOT NULL,
    [DiagOrderID] [int] NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].[ActionCompleted](
    [ACID] [int] IDENTITY(1,1) NOT NULL,
    [ActionID] [int] NOT NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].[ActionCancelled](
    [ACAID] [int] IDENTITY(1,1) NOT NULL,
    [ActionID] [int] NOT NULL,
    [CancelledByPhysician] [int] NULL,
 ) ON [PRIMARY]

解决方案

When using code first, it's always useful to at least try letting EF create the database for you - this is the default behavior (will look for local instance of SQL Server called .\SQLEXPRESS). If you create the database manually and want to control the column names, which is what seems like is happening, you'll need to specify the column name with the fluent API. See http://msdn.microsoft.com/en-us/library/hh295845(v=vs.103).aspx and http://msdn.microsoft.com/en-us/library/hh295847(v=VS.103).aspx for some examples that should cover what you're getting stuck on.

Just for reference, the database you've created would have the following tables and column names:

CREATE TABLE [dbo].[ActionCancelled](
    [ActionID] [int] NOT NULL,
    [CancelledByPhysician] [int] NOT NULL,
PRIMARY KEY CLUSTERED ( [ActionID] ASC )
)

CREATE TABLE [dbo].[ActionCompleted](
    [ActionID] [int] NOT NULL,
PRIMARY KEY CLUSTERED ( [ActionID] ASC )
)

CREATE TABLE [dbo].[Actions](
    [ActionID] [int] IDENTITY(1,1) NOT NULL,
    [ActionDT] [datetime] NOT NULL,
    [Order_OrderId] [int] NULL,
PRIMARY KEY CLUSTERED ( [ActionID] ASC )
)

CREATE TABLE [dbo].[Orders](
    [OrderId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](max) NULL,
    [ACO_ActionID] [int] NULL,
    [ACA_ActionID] [int] NULL,
PRIMARY KEY CLUSTERED ( [OrderId] ASC )
)

这篇关于实体框架 - 继承 - 零对一关系到子对象,如何映射? (流利的API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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