Entity Framework: can Include method work with a View?

103 Views Asked by At

I have a View in my project and I would like to retrieve all data from a table whose Ids is in the View.

Below the script to create a very simplified database (View_1 and Motors and the same but in real case View_1 is more complex):

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

These are my classes:

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

I would like to retrieve Motors from View_1 by Include method in this way:

View_1.Include(x => x.Motors)

is it possible? Any advice?

1

There are 1 best solutions below

0
Emanuele Lacopo On

I changed my code according to the design pattern described in this 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);
}

and now I can navigate in both classes.