gpt4 book ai didi

entity-framework - Entity Framework 和 SQL Server View

转载 作者:行者123 更新时间:2023-12-04 07:25:35 24 4
gpt4 key购买 nike

出于几个我无法自由谈论的原因,我们在我们的 Sql Server 2005 数据库上定义一个 View ,如下所示:

CREATE VIEW [dbo].[MeterProvingStatisticsPoint]
AS
SELECT
CAST(0 AS BIGINT) AS 'RowNumber',
CAST(0 AS BIGINT) AS 'ProverTicketId',
CAST(0 AS INT) AS 'ReportNumber',
GETDATE() AS 'CompletedDateTime',
CAST(1.1 AS float) AS 'MeterFactor',
CAST(1.1 AS float) AS 'Density',
CAST(1.1 AS float) AS 'FlowRate',
CAST(1.1 AS float) AS 'Average',
CAST(1.1 AS float) AS 'StandardDeviation',
CAST(1.1 AS float) AS 'MeanPlus2XStandardDeviation',
CAST(1.1 AS float) AS 'MeanMinus2XStandardDeviation'
WHERE 0 = 1

这个想法是 Entity Framework 将根据这个查询创建一个实体,它确实这样做了,但它会生成一个错误,指出以下内容:

Warning 6002: The table/view 'Keystone_Local.dbo.MeterProvingStatisticsPoint' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.



它决定 CompletedDateTime 字段将是该实体的主键。

我们使用 EdmGen 来生成模型。有没有办法不让 Entity Framework 包含此 View 的任何字段作为主键?

最佳答案

我们遇到了同样的问题,这是解决方案:

要强制 Entity Framework 使用列作为主键,请使用 ISNULL。

要强制 Entity Framework 不使用列作为主键,请使用 NULLIF。

应用它的一个简单方法是将 View 的 select 语句包装在另一个 select 中。

例子:

SELECT
ISNULL(MyPrimaryID,-999) MyPrimaryID,
NULLIF(AnotherProperty,'') AnotherProperty
FROM ( ... ) AS temp

关于entity-framework - Entity Framework 和 SQL Server View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com