实体框架:Include方法可以与视图一起使用吗



我的项目中有一个View,我想从ID在View中的表中检索所有数据。

在脚本下面创建一个非常简化的数据库(View_1和Motors相同,但在实际情况下View_1更复杂(:

USE [LacTest]
GO
/****** Object:  Table [dbo].[Motors]    Script Date: 11/16/2021 4:57:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Motors](
[MotorId] [int] IDENTITY(1,1) NOT NULL,
[CompanyId] [int] NOT NULL,
[TotSales1] [int] NOT NULL,
[TotSales2] [int] NOT NULL,
CONSTRAINT [PK_Motors] PRIMARY KEY CLUSTERED 
(
[MotorId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[LacCompanies]    Script Date: 11/16/2021 4:57:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[LacCompanies](
[CompanyId] [int] IDENTITY(1,1) NOT NULL,
[Tot1] [int] NOT NULL,
[Tot2] [int] NOT NULL,
CONSTRAINT [PK_Companies] PRIMARY KEY CLUSTERED 
(
[CompanyId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[LacCompanies] ON 
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (1, 300, 200)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (2, 400, 100)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (3, 500, 100)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (4, 600, 200)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (5, 700, 500)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (6, 800, 400)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (7, 900, 300)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (8, 50, 20)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (9, 80, 20)
GO
INSERT [dbo].[LacCompanies] ([CompanyId], [Tot1], [Tot2]) VALUES (10, 40, 10)
GO
SET IDENTITY_INSERT [dbo].[LacCompanies] OFF
GO
SET IDENTITY_INSERT [dbo].[Motors] ON 
GO
INSERT [dbo].[Motors] ([MotorId], [CompanyId], [TotSales1], [TotSales2]) VALUES (1, 4, 35, 23)
GO
INSERT [dbo].[Motors] ([MotorId], [CompanyId], [TotSales1], [TotSales2]) VALUES (2, 5, 140, 70)
GO
INSERT [dbo].[Motors] ([MotorId], [CompanyId], [TotSales1], [TotSales2]) VALUES (3, 7, 200, 24)
GO
INSERT [dbo].[Motors] ([MotorId], [CompanyId], [TotSales1], [TotSales2]) VALUES (4, 9, 2, 1)
GO
SET IDENTITY_INSERT [dbo].[Motors] OFF
GO
ALTER TABLE [dbo].[Motors]  WITH CHECK ADD  CONSTRAINT [FK_Motors_Companies] FOREIGN KEY([CompanyId])
REFERENCES [dbo].[LacCompanies] ([CompanyId])
GO
ALTER TABLE [dbo].[Motors] CHECK CONSTRAINT [FK_Motors_Companies]
GO
/****** Object:  View [dbo].[View_1]    Script Date: 11/16/2021 4:57:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[View_1]
AS
SELECT    MotorId, CompanyId, TotSales1, TotSales2
FROM         dbo.Motors
GO

这些是我的课程:

public class Motors
{
[Key]
public int MotorId { get; set; }
[ForeignKey("CompanyId")]
public LacCompanies Company { get; set; }
public int TotSales1 { get; set; }
public int TotSales2 { get; set; }
}
public class LacCompanies
{
[Key]
public int CompanyId { get; set; }
public int Tot1 { get; set; }
public int Tot2 { get; set; }
public virtual Motors Motors { get; set; }
}
public class View_1
{
public int MotorId { get; set; }
public int CompanyId { get; set; }
public int TotSales1 { get; set; }
public int TotSales2 { get; set; }
public virtual Motors Motors { get; set; }
}

我想通过Include方法从View_1中检索电机,方法如下:

View_1.Include(x => x.Motors)

有可能吗?有什么建议吗?

我根据本文中描述的设计模式更改了代码:

public class View_1{
public int MotorId { get; set; }
public int CompanyId { get; set; }
public virtual Motors Motor { get; set; }
public virtual LacCompanies Company { get; set; }
}
modelBuilder.Entity<View_1>(entity => {
entity.HasNoKey();
entity.ToView("View_1");
entity.HasOne(e => e.Company)
.WithOne()
.HasForeignKey<View_1>(e => e.CompanyId);
entity.HasOne(e => e.Motor)
.WithOne()
.HasForeignKey<View_1>(e => e.MotorId);
}

现在我可以在两个类中导航了。

最新更新