1+ using System . Collections . Generic ;
2+ using System . ComponentModel . DataAnnotations . Schema ;
3+ using Microsoft . EntityFrameworkCore ;
4+
5+ namespace EntityFrameworkCoreDemo . Ignore
6+ {
7+ public class IgnoreDbContext : DbContext
8+ {
9+ public DbSet < User > Users { get ; set ; }
10+
11+ public IgnoreDbContext ( )
12+ {
13+ this . AddSmartInspectLogs ( ) ;
14+ this . Database . EnsureCreated ( ) ;
15+ }
16+
17+ protected override void OnConfiguring ( DbContextOptionsBuilder optionsBuilder )
18+ {
19+ optionsBuilder . UseSqlServer ( @"Server=(localdb)\mssqllocaldb;Database=3_IgnoreDatabase;Trusted_Connection=True;" ) ;
20+ }
21+
22+ protected override void OnModelCreating ( ModelBuilder modelBuilder )
23+ {
24+ //// Excluding data type with Fluent API.
25+ //modelBuilder.Ignore<IgnoredUser>();
26+
27+ //// Excluding properties with Fluent API.
28+ //modelBuilder.Entity<User>().Ignore(b => b.LastName);
29+ }
30+ }
31+
32+ public class User
33+ {
34+ public int Id { get ; set ; }
35+
36+ public string FirstName { get ; set ; }
37+
38+ // Excluding properties with data Annotations.
39+ [ NotMapped ]
40+ public string LastName { get ; set ; }
41+ }
42+
43+ // Excluding data type with data Annotations.
44+ [ NotMapped ]
45+ public class IgnoredUser
46+ {
47+ public int Id { get ; set ; }
48+
49+ public string Name { get ; set ; }
50+
51+ public string Url { get ; set ; }
52+
53+ public ICollection < int > Permissions { get ; set ; }
54+ }
55+ }
0 commit comments