简体   繁体   English

model如何在EDMX中建立非标准关系?

[英]How to model a non-standard relationship in EDMX?

I'm trying to model a somewhat non-standard relationship using .NET Entity Framework 4 for consumption over a RESTful WCF Data Service.我正在尝试使用 .NET 实体框架 4 来 model 有点非标准的关系,以便在 RESTful WCF 数据服务上使用。

Consider the following simplified data structure where metadata is stored in a single table, but may be associated to different entities stored in different tables (identified by the TYPE column)考虑以下简化的数据结构,其中元数据存储在单个表中,但可能与存储在不同表中的不同实体相关联(由 TYPE 列标识)

METADATA Table
--------------
ID      FK_ID   TYPE        VALUE
--      ----    -----       -----
1       100     PRODUCT     Foo
2       101     PRODUCT     Foo
3       101     SERVICE     Bar
4       102     SERVICE     Bar

PRODUCT Table
-------------
ID      Name
--      ----
100     A
101     B
102     C
103     D

SERVICE Table
-------------
ID      Name
--      ----
100     W
101     X   
102     Y
103     Z

The problem I am facing is that I want to create a property我面临的问题是我想创建一个属性

Product.List<Metadata> 

for the Product object and the Service object.对于产品 object 和服务 object。 Since they are not associated with a single FK to a single table I don't know how I can model this relationship in an EDMX file.由于它们与单个表的单个 FK 没有关联,我不知道如何在 EDMX 文件中实现 model 这种关系。

My end goal is to be able to call a method on a WCF Data Service, and return a JSON response that has a Product serialized with a list of its Metadata included.我的最终目标是能够在 WCF 数据服务上调用方法,并返回 JSON 响应,其中包含序列化的产品及其元数据列表。

One way to do this would be to create either a computed column or a view with an additional column for each of the tables that has metadata and populate the additional column with the name of the table.一种方法是为每个具有元数据的表创建计算列或具有附加列的视图,并使用表的名称填充附加列。

CREATE VIEW PRODUCT_VIEW AS
    Select ID, Name, "PRODUCT" as Type From PRODUCT

Or或者

CREATE TABLE PRODUCT(
  ID int,
  Name varchar(20),
  Type AS "PRODUCT"
)

Then you can have a FK relationship between PRODUCT_VIEW/PRODUCT and METADATA on the fields FK_ID and Type.然后,您可以在字段 FK_ID 和 Type 上建立 PRODUCT_VIEW/PRODUCT 和 METADATA 之间的 FK 关系。 These two fields could then be mapped as a complex type and a relationship between the METADATA table and PRODUCT_VIEW can be formed in EF.然后可以将这两个字段映射为复杂类型,并且可以在 EF 中形成 METADATA 表和 PRODUCT_VIEW 之间的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM