在app中使用从EF代码首次迁移创建的4个视图。此视图中的字段名称相同,类型相同。
为了在app的BL中使用这个视图,创建了基类和4个子类。
这用于UNION ALL
个视图。
父类:
public abstract class CdcBase : EntityBase
{
public Guid Guid { get; set; }
public int YearBalance { get; set; }
public int Typezalezh { get; set; }
public string License { get; set; }
public string ObjectName { get; set; }
public string ZalezhName { get; set; }
public string PlastName { get; set; }
public string Category { get; set; }
public string Parameter { get; set; }
public string LastVal { get; set; }
public string Val { get; set; }
public int Operation { get; set; }
public DateTime EndT { get; set; }
}
例如,下面是使用view_CDC_1
的四个类中的一个:
[Table("view_CDC_1)]
public class CdcResursGas : CdcBase
{
}
在BL中使用repository
和Unit Of Work
模式。
当我尝试使用以下内容从视图中选择数据时:
var result = _myReposytory.GetAll().ToList();
但是要抓住以下错误:
InnerException:
Class=16
ErrorCode=-2146232060
HResult=-2146232060
LineNumber=4
Message=Invalid column name 'Id'. Invalid column name 'Id'. Invalid column name 'Id'. Invalid column name 'Id'.
Number=207
Procedure=""
Server=SPB33-IT-W-5557
Source=.Net SqlClient Data Provider
State=1
StackTrace:
EF生成的奇怪查询:
{SELECT
CASE WHEN ([UnionAll3].[C1] = 1) THEN '0X0X' WHEN ([UnionAll3].[C2] = 1) THEN '0X1X' WHEN ([UnionAll3].[C3] = 1) THEN '0X2X' ELSE '0X3X' END AS [C1],
[UnionAll3].[Id] AS [C2],
[Extent5].[Guid] AS [Guid],
[Extent5].[YearBalance] AS [YearBalance],
[Extent5].[Typezalezh] AS [Typezalezh],
[Extent5].[License] AS [License],
[Extent5].[ObjectName] AS [ObjectName],
[Extent5].[ZalezhName] AS [ZalezhName],
[Extent5].[PlastName] AS [PlastName],
[Extent5].[Category] AS [Category],
[Extent5].[Parameter] AS [Parameter],
[Extent5].[LastVal] AS [LastVal],
[Extent5].[Val] AS [Val],
[Extent5].[Operation] AS [Operation],
[Extent5].[EndT] AS [EndT]
FROM (SELECT
[Extent1].[Id] AS [Id],
cast(1 as bit) AS [C1],
cast(0 as bit) AS [C2],
cast(0 as bit) AS [C3]
FROM [dbo].[view_1] AS [Extent1]
UNION ALL
SELECT
[Extent2].[Id] AS [Id],
cast(0 as bit) AS [C1],
cast(0 as bit) AS [C2],
cast(0 as bit) AS [C3]
FROM [dbo].[view_2] AS [Extent2]
UNION ALL
SELECT
[Extent3].[Id] AS [Id],
cast(0 as bit) AS [C1],
cast(1 as bit) AS [C2],
cast(0 as bit) AS [C3]
FROM [dbo].[view_3] AS [Extent3]
UNION ALL
SELECT
[Extent4].[Id] AS [Id],
cast(0 as bit) AS [C1],
cast(0 as bit) AS [C2],
cast(1 as bit) AS [C3]
FROM [dbo].[view_4] AS [Extent4]) AS [UnionAll3]
INNER JOIN [dbo].[CdcBases] AS [Extent5] ON [UnionAll3].[Id] = [Extent5].[Id]}
我不明白为什么在这里cast
。我也看到[dbo].[CdcBases]
,但数据库没有表CdcBases
。
但正如我所料,此查询包含视图的UNION ALL。我做错了什么?也许我必须创建4个没有继承并使用LINQ的实体?