当从视图中推断出没有密钥时添加实体密钥

时间:2011-12-11 18:39:53

标签: entity-framework ado.net

我有一个数据库视图,它连接SQL Server 2005中的许多表。它看起来像这样:

SELECT     
   m1.MenuName AS menu_name, m2.MenuName AS sub_menu_name, p.ProductName, od.amount
FROM         
   dbo.tblMenus AS m1 
FULL OUTER JOIN
   dbo.tblMenus AS m2 
FULL OUTER JOIN
   dbo.tblProductsRelMenus AS pm ON m2.Id = pm.SubMenuId ON m1.Id = pm.MenuId 
FULL OUTER JOIN
   (SELECT     
       dbo.tblOrderDetails.ProductId, SUM(dbo.tblOrderDetails.Ammount) AS amount
    FROM          
       dbo.tblOrderDetails 
    FULL OUTER JOIN
       dbo.tblOrders AS o ON dbo.tblOrderDetails.OrderId = o.OrderId
    WHERE (o.OrderDestroyed = 0)
    GROUP BY dbo.tblOrderDetails.ProductId) AS od 
RIGHT OUTER JOIN
   dbo.tblProducts AS p ON od.ProductId = p.ProductId ON pm.ProductId = p.ProductId

当我尝试创建ADO .Net实体数据模型时,它抱怨SSDL中没有主键。然后我发现了这个:

http://msdn.microsoft.com/en-us/library/dd163156.aspx

但我不理解有关定义查询的部分。当然我只想要一个具有唯一数字的列来定义密钥,或者?

  <EntityType Name="SoldItemsView">
    <Key>
       <PropertyRef Name="SoldItemsViewId" />
    </Key>
    <Property Name="SoldItemsView" Type="int" Nullable="false" />
    <Property Name="menu_name" Type="nvarchar" MaxLength="100" />
    <Property Name="sub_menu_name" Type="nvarchar" MaxLength="100" />
    <Property Name="ProductName" Type="nvarchar" MaxLength="50" />
    <Property Name="amount" Type="int" />
  </EntityType>

但是如何使用唯一数字填充此列?

谢谢,

百里

1 个答案:

答案 0 :(得分:0)

您只能使用视图中的列。要定义实体键,必须从视图中选择列(或列集),以从视图中唯一标识记录。