我尝试映射以下类结构和匹配的数据库结构。我有一个包含元素列表的设计类。可以有 3 种不同类型的元素:文本、形状和图像。我使用抽象类来维护形状之间的公共属性,我的数据库结构反映了这一点。
我的存储过程返回两个记录集,一个包含设计,另一个包含所有元素。元素记录集使用左连接返回所有元素及其非公共属性。每个元素都有一个 ElementTyepID,它决定了它是什么类型的元素。我的设计类包含一个列表。有没有办法获取第二个结果集并根据 ElementTypeID 将其映射到正确的 Element 子类?
public class Design
{
public int DesignID {get;set;}
public string DesignName {get;set;}
public List<Element> ElementList {get;set;}
}
public class Element
{
public int ElementID {get;set;}
public int DesignID {get;set;}
public int ElementTypeID {get;set;}
public int Width {get;set;}
public int Height {get;set;}
public string Color {get;set;}
}
public class ElementText
{
public int ElementTextID {get;set;}
public int ElementID {get;set;}
public string Font {get;set;}
public string TextValue {get;set;}
}
public class ElementShape
{
public int ElementShapeID {get;set;}
public int ElementID {get;set;}
public int ShapeType {get;set;}
}
public class ElementImage
{
public int ElementImageID int {get;set;}
public int ElementID {get;set;}
public string ImageURL {get;set;}
}
Table: Design
-------------
DesignID int
DesignName varchar(50)
Table: Element
--------------
ElementID int
DesignID int
ElementTypeID int 0-Text, 1-Shape, 2-Image
Width int
Height int
Color varchar(10)
Table: ElementText
------------------
ElementTextID int
ElementID int
Font varchar(50)
TextValue varchar(50)
Table: ElementShape
-------------------
ElementShapeID int
ElementID int
ShapeType int
Table: ElementImage
-------------------
ElementImageID int
ElementID int
ImageURL varchar(255)
Create Table #Designs
(
[DesignID] [int] NOT NULL,
[DesignName] [varchar](50) NOT NULL,
)
-- Place all designs into a temp table
Insert Into #Designs
Select DesignID, DesignName from Design
-- Return 1st Result Set (Designs)
select * from #Designs
-- Return 2nd Result Set (Elements)
select * from Element as e
left Join ElementText as t on e.ElementID=t.ElementID
left Join ElementShape as s on e.ElementID=s.ElementID
left Join ElementImage as c on e.ElementID=c.ElementID
Where e.DesignID in (Select DesignID from #Designs)
Order By e.ElementID ASC