Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Last active January 21, 2023 19:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Entity Framework 7 GetType() examples
USE [EF7Inheritance]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Blogs]') AND type in (N'U'))
DROP TABLE [dbo].[Blogs]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Blogs](
[BlogId] [int] IDENTITY(1,1) NOT NULL,
[Url] [nvarchar](max) NOT NULL,
[Discriminator] [nvarchar](max) NOT NULL,
[Password] [nvarchar](max) NULL,
[mfaOn] [bit] NULL,
[RssUrl] [nvarchar](max) NULL,
CONSTRAINT [PK_Blogs] PRIMARY KEY CLUSTERED
(
[BlogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Blogs] ON
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (1, N'Url1', N'Blog', NULL, NULL, NULL)
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (2, N'Url2', N'Blog', NULL, NULL, NULL)
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (3, N'Url3', N'RssBlog', NULL, NULL, N'RssUrl1')
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (4, N'Url4', N'RssBlog', NULL, NULL, N'RssUrl2')
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (5, N'Url5', N'PrivateBlog', N'password1', NULL, NULL)
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (6, N'Url6', N'PrivateBlog', N'admin', NULL, NULL)
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (7, N'Url7', N'PrivateBlogWithMFA', N'pAsSwOrD', 1, NULL)
GO
INSERT [dbo].[Blogs] ([BlogId], [Url], [Discriminator], [Password], [mfaOn], [RssUrl]) VALUES (8, N'Url8', N'PrivateBlogWithMFA', N'123456', 0, NULL)
GO
SET IDENTITY_INSERT [dbo].[Blogs] OFF
GO
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
public class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<RssBlog> RssBlogs { get; set; }
public DbSet<PrivateBlog> PrivateBlogs { get; set; }
public DbSet<PrivateBlogWithMFA> PrivateBlogsWithMFA { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EF7Inheritance;Trusted_Connection=True")
.EnableSensitiveDataLogging()
.LogTo(
s => Console.WriteLine(s), LogLevel.Information);
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
public class RssBlog : Blog
{
public string RssUrl { get; set; }
}
public class PrivateBlog : Blog
{
public string Password { get; set; }
}
public class PrivateBlogWithMFA : PrivateBlog
{
public bool mfaOn { get; set; }
}
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace EF7Benchmarks
{
internal class Program
{
static void Main(string[] args)
{
var context = new MyContext();
//all PrivateBlogs including derived types
//and then filter on CLIENT SIDE (bad for perf)
Debug.WriteLine(context.PrivateBlogs.ToQueryString());
//all PrivateBlogs excluding specific derived type(s) and descendants
//Gets cumbersome if multiple derived types
Debug.WriteLine(context.PrivateBlogs.
Where(p => !(p is PrivateBlogWithMFA)).ToQueryString());
//only PrivateBlogs specifically, not derived types
//Does the job but syntax a little ugly.
Debug.WriteLine(context.PrivateBlogs.
Where(v => EF.Property<string>(v, "Discriminator")
== nameof(PrivateBlog)).ToQueryString());
//only PrivateBlogs specifically, not derived types
Debug.WriteLine(context.PrivateBlogs.
Where(x =>
x.GetType() == typeof(PrivateBlog)).ToQueryString());
//PrivateBlog and RssBlog specifically but not dervied types
Debug.WriteLine(context.Blogs.
Where(x =>
x.GetType() == typeof(PrivateBlog) ||
x.GetType() == typeof(RssBlog)).ToQueryString());
//all except PrivateBlog (will return derived types of PrivateBlog)
Debug.WriteLine(context.Blogs.
Where(x =>
x.GetType() != typeof(PrivateBlog)).ToQueryString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment