【问题标题】:Entity Framework: can Include method work with a View?实体框架:Include 方法可以与视图一起使用吗?
【发布时间】:2021-12-27 18:09:10
【问题描述】:

我的项目中有一个视图,我想从 ID 位于视图中的表中检索所有数据。

在脚本下方创建一个非常简化的数据库(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 中检索 Motors:

View_1.Include(x => x.Motors)

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

【问题讨论】:

  • 试试吧,如果您有问题,请告诉我们。我们怎么知道呢?
  • 我尝试了几种解决方案,但都没有奏效,所以我认为 Include 方法不能与 View 一起使用。我在文档中找不到任何内容,所以我写了这篇文章,你能帮帮我吗?

标签: view entity-framework-core include entity-framework-5


【解决方案1】:

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

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);
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2015-04-26
    相关资源
    最近更新 更多