我将 SQL Server 2008 和实体框架用于我正在开发的应用程序。
我已经在 SQL Server 中的所有表上设置了主键,因为 EF 需要它,但是我对一个视图有问题。由于它没有主键,我使用 ROW_NUMBER() 函数来获取唯一的行标识符。我希望该列被定义为 NOT NULL,因此我可以将其用作 EF 中的主键,但相反,该列可以为空。
这是我的看法:
CREATE VIEW [dbo].[vw_Action]
AS
SELECT *, ROW_NUMBER() OVER(ORDER BY ActionDate DESC) AS RowID
FROM
(
SELECT User1ID AS UserID, NULL AS FriendID, Object1ID, Object2ID, ActionType, ActionDate
FROM vw_ActionBase
WHERE ActionType = 1
GROUP BY User1ID, Object1ID, Object2ID, ActionType, ActionDate
UNION
SELECT User1ID AS UserID, NULL AS FriendID, Object1ID, Object2ID, ActionType, ActionDate
FROM vw_ActionBase
WHERE ActionType = 2
GROUP BY User1ID, Object1ID, Object2ID, ActionType, ActionDate
UNION
SELECT User1ID AS UserID, NULL AS FriendID, Object1ID, Object2ID, ActionType, ActionDate
FROM vw_ActionBase
WHERE ActionType = 3
GROUP BY User1ID, Object1ID, Object2ID, ActionType, Object1ID, ActionDate
)x
所以,我的问题是如何将 ROWID 列设置为 NOT NULL,所以我可以将它用作 EF 中的主键。
提前致谢!