Entity framework 实体化视图未在向导中显示

Entity framework materialized view not showing in wizard

我正在使用 PostgreSQL 12 和 EF6。我正在尝试将实体化视图添加到我的模型中,但该视图未在模型向导中列出。我知道要能够将视图添加到模型中,Entity Framework 至少需要数据库视图中的一列不可为空。但是我不知道该怎么做。

我试过创建唯一索引和行号,但都没有成功。

CREATE MATERIALIZED VIEW "Stock"
AS

SELECT coalesce(cast(ROW_NUMBER() OVER() as int), 0) as "Id",
    "PartMaster"."PartID",
    "OEMPart"."PartCatalogID",
    "PartMaster"."PartCode",
    "PartCatalog"."CustomPartCode",
    "OEMPart"."OEMCode",
    "PartMaster"."PartDesc",
    "GoodsReceivedDetail"."Cost",
    "GoodsReceivedDetail"."Markup",
    "GoodsReceivedDetail"."SellingPrice",
    coalesce("GoodsReceivedDetail"."Quantity", 0) as "InQuantity",
    coalesce("InvoiceDetail"."Quantity", 0) as "OutQuantity", 
    coalesce("GoodsReceivedDetail"."Quantity", 0) - coalesce("InvoiceDetail"."Quantity", 0) as "StockQuantity"
FROM
    "PartCatalog"
INNER JOIN
    "PartMaster"
ON
    (
        "PartCatalog"."PartID" = "PartMaster"."PartID")
INNER JOIN
    "OEMPart"
ON
    (
        "PartCatalog"."PartID" = "OEMPart"."PartID")
INNER JOIN
    "GoodsReceivedDetail"
ON
    (
        "OEMPart"."PartCatalogID" = "GoodsReceivedDetail"."PartCatalogID")
LEFT OUTER JOIN
    "InvoiceDetail"
ON
    (
        "GoodsReceivedDetail"."PartCatalogID" = "InvoiceDetail"."PartCatalogID")
INNER JOIN
    "PartCategory"
ON
    (
        "PartMaster"."PartCategoryID" = "PartCategory"."PartCategoryID") ;
        
CREATE UNIQUE INDEX "Id"
  ON "Stock" ("PartCatalogID");

要使该对象可见,只需向您的数据库添加一个视图。

CREATE VIEW stock_view
AS select * from Stock

注意:物化视图用于提高查询性能。如果您没有性能问题,请坚持使用常规视图。只需删除 materialized.

一词,即可将您的原始实体化视图重写为常规视图

要回答有关如何使 table Stock 中的列 Id 不可为空的原始问题,您可以将答案组合在 this Stack Overflow post with this one如下:

UPDATE pg_catalog.pg_attribute 
SET attnotnull = true
WHERE attrelid = Stock::regclass::oid
 and attname  = 'Id'