我有以下架构:
我尝试创建一个视图并尽一切努力获得IDStore_CardStore
列NOT NULL
,以使其成为我EF实体的主键,但没有成功:
我的目标是选择表StockCard
中的所有内容,即使StockCardStores
中不存在,但我需要保持IDStore_CardStore
不为空且唯一
SELECT ISNULL(dbo.StockCardsStores.IDStore_CardStore, NEWID()) AS IDStore_CardStore ,
NULLIF(dbo.StockCard.IDStockCardIndex, NEWID()) AS IDStockCardIndex ,
dbo.Stores.StoreName ,
dbo.StockCardsStores.IDPurchaseInvoice ,
NULLIF(dbo.StockCard.Designation,'')
FROM dbo.Stores
INNER JOIN dbo.StockCardsStores ON dbo.Stores.IDStore = dbo.StockCardsStores.IDStore
RIGHT OUTER JOIN dbo.StockCard ON dbo.StockCardsStores.IDStockCardIndex = dbo.StockCard.IDStockCardIndex
还有其他解决方法!
答案 0 :(得分:1)
我认为IDStore_CardStore已经不能为null,因为它是StockCardStores表中的主键。也许问题是IDStore_CardStore可能在您的视图中有重复的值,因此不可能成为视图的主键。
我不知道您的要求,但您可以在查询中为您的视图伪造一个主键:
SELECT ROW_NUMBER() OVER (ORDER BY dbo.StockCardsStores.IDStore_CardStore DESC) AS Id_PK, ISNULL(dbo.StockCardsStores.IDStore_CardStore, NEWID()) AS IDStore_CardStore ,
NULLIF(dbo.StockCard.IDStockCardIndex, NEWID()) AS IDStockCardIndex ,
dbo.Stores.StoreName ,
dbo.StockCardsStores.IDPurchaseInvoice ,
NULLIF(dbo.StockCard.Designation,'')
FROM dbo.Stores
INNER JOIN dbo.StockCardsStores ON dbo.Stores.IDStore = dbo.StockCardsStores.IDStore
RIGHT OUTER JOIN dbo.StockCard ON dbo.StockCardsStores.IDStockCardIndex = dbo.StockCard.IDStockCardIndex
然后ID_PK可以是他在EF中查看的PK。