Skip to content

Commit d980ab9

Browse files
committed
Adding the code
first check in.
0 parents  commit d980ab9

22 files changed

+1136
-0
lines changed

EntityFrameworkCoreDemo.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26730.16
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCoreDemo", "EntityFrameworkCoreDemo\EntityFrameworkCoreDemo.csproj", "{245C7726-55BE-462E-8A19-ACF6DEEF9AB2}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{245C7726-55BE-462E-8A19-ACF6DEEF9AB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{245C7726-55BE-462E-8A19-ACF6DEEF9AB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{245C7726-55BE-462E-8A19-ACF6DEEF9AB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{245C7726-55BE-462E-8A19-ACF6DEEF9AB2}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C1C81F27-66D4-4A30-B86A-0894FCC40413}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace EntityFrameworkCoreDemo.SimpleExample
5+
{
6+
public class SimpleExampleDbContext : DbContext
7+
{
8+
public DbSet<Blog> Blogs { get; set; }
9+
10+
public DbSet<Post> Posts { get; set; }
11+
12+
public SimpleExampleDbContext()
13+
{
14+
this.AddSmartInspectLogs();
15+
this.Database.EnsureCreated();
16+
}
17+
18+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
19+
{
20+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=1_SimpleExampleDatabase;Trusted_Connection=True;");
21+
// optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);
22+
}
23+
}
24+
25+
public class Blog
26+
{
27+
public int BlogId { get; set; }
28+
29+
public string Url { get; set; }
30+
31+
public ICollection<Post> Posts { get; set; }
32+
}
33+
34+
public class Post
35+
{
36+
public int PostId { get; set; }
37+
38+
public string Title { get; set; }
39+
40+
public string Content { get; set; }
41+
42+
public int BlogId { get; set; }
43+
44+
public Blog Blog { get; set; }
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace EntityFrameworkCoreDemo.PrimaryKey
5+
{
6+
public class PrimaryKeyDbContext : DbContext
7+
{
8+
public DbSet<User> Users { get; set; }
9+
10+
public PrimaryKeyDbContext()
11+
{
12+
this.AddSmartInspectLogs();
13+
this.Database.EnsureCreated();
14+
}
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
17+
{
18+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=2_PrimaryKeyDatabase;Trusted_Connection=True;");
19+
}
20+
21+
protected override void OnModelCreating(ModelBuilder modelBuilder)
22+
{
23+
// // Define the primary key Fluent API.
24+
// modelBuilder.Entity<User>().HasKey(c => c.Id);
25+
26+
// // Define the composite primary key Fluent API.
27+
// modelBuilder.Entity<UserPrimaryKeyDemo>().HasKey(c => new { c.Id, c.EMail });
28+
}
29+
}
30+
31+
public class User
32+
{
33+
// Define the primary key data Annotations.
34+
[Key]
35+
// [Column(Order = 0), Key]
36+
public int Id { get; set; }
37+
38+
// Define the composite primary key Fluent API.
39+
// [Column(Order = 1), Key]
40+
// Set required and maximum Length with Fluent API.
41+
// [Required, MaxLength(500)]
42+
public string EMail { get; set; }
43+
}
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.ComponentModel.DataAnnotations.Schema;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace EntityFrameworkCoreDemo.HasColumnType
5+
{
6+
public class HasColumnTypeDbContext : DbContext
7+
{
8+
public DbSet<User> Users { get; set; }
9+
10+
public HasColumnTypeDbContext()
11+
{
12+
this.AddSmartInspectLogs();
13+
this.Database.EnsureCreated();
14+
}
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
17+
{
18+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=4_HasColumnTypeDatabase;Trusted_Connection=True;");
19+
}
20+
21+
protected override void OnModelCreating(ModelBuilder modelBuilder)
22+
{
23+
//// Define data types with Fluent API.
24+
//modelBuilder.Entity<User>().Property(b => b.FirstName).HasColumnType("varchar(200)");
25+
}
26+
}
27+
28+
public class User
29+
{
30+
public int Id { get; set; }
31+
32+
[Column(TypeName = "varchar(200)")]
33+
public string FirstName { get; set; }
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace EntityFrameworkCoreDemo.DefaultValues
4+
{
5+
public class DefaultValuesDbContext : DbContext
6+
{
7+
public DbSet<User> Users { get; set; }
8+
9+
public DefaultValuesDbContext()
10+
{
11+
this.AddSmartInspectLogs();
12+
this.Database.EnsureCreated();
13+
}
14+
15+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
16+
{
17+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=5_DefaultValuesDatabase;Trusted_Connection=True;");
18+
}
19+
20+
protected override void OnModelCreating(ModelBuilder modelBuilder)
21+
{
22+
//// Define default values with Fluent API.
23+
//modelBuilder.Entity<User>().Property(b => b.FirstName).HasDefaultValue("Bassam");
24+
}
25+
}
26+
27+
public class User
28+
{
29+
public int Id { get; set; }
30+
31+
// Define default with code.
32+
public string FirstName { get; set; } = "Bassam";
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace EntityFrameworkCoreDemo.Uniqueness
4+
{
5+
public class UniquenessDbContext : DbContext
6+
{
7+
public DbSet<User> Users { get; set; }
8+
9+
public UniquenessDbContext()
10+
{
11+
this.AddSmartInspectLogs();
12+
this.Database.EnsureCreated();
13+
}
14+
15+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
16+
{
17+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=6_UniquenessDatabase;Trusted_Connection=True;");
18+
}
19+
20+
protected override void OnModelCreating(ModelBuilder modelBuilder)
21+
{
22+
// Indexes with fluent API.
23+
modelBuilder.Entity<User>().HasAlternateKey(c => c.FirstName).HasName("IX_FirstName_Unique");
24+
modelBuilder.Entity<User>().HasIndex(b => b.LastName).IsUnique();
25+
}
26+
}
27+
28+
public class User
29+
{
30+
public int Id { get; set; }
31+
32+
// Indexes with data Annotations, see the StackOverflow Link.
33+
public string FirstName { get; set; }
34+
public string LastName { get; set; }
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace EntityFrameworkCoreDemo.RequiredAndMaxLength
5+
{
6+
public class RequiredAndMaxLengthDbContext : DbContext
7+
{
8+
public DbSet<User> Users { get; set; }
9+
10+
public RequiredAndMaxLengthDbContext()
11+
{
12+
this.AddSmartInspectLogs();
13+
this.Database.EnsureCreated();
14+
}
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
17+
{
18+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=7_RequiredAndMaxLengthDatabase;Trusted_Connection=True;");
19+
}
20+
21+
protected override void OnModelCreating(ModelBuilder modelBuilder)
22+
{
23+
//// Set required and maximum Length with Fluent API.
24+
//modelBuilder.Entity<User>().Property(b => b.FirstName).IsRequired().HasMaxLength(500);
25+
}
26+
}
27+
28+
public class User
29+
{
30+
public int Id { get; set; }
31+
32+
[Required, MaxLength(500)]
33+
public string FirstName { get; set; }
34+
}
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
4+
namespace EntityFrameworkCoreDemo.DatabaseGeneratedOption
5+
{
6+
public class DatabaseGeneratedOption : DbContext
7+
{
8+
public DbSet<User> Users { get; set; }
9+
10+
public DatabaseGeneratedOption()
11+
{
12+
this.AddSmartInspectLogs();
13+
this.Database.EnsureCreated();
14+
}
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
17+
{
18+
19+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=8_DatabaseGeneratedOptionDatabase;Trusted_Connection=True;");
20+
}
21+
22+
protected override void OnModelCreating(ModelBuilder modelBuilder)
23+
{
24+
//// Value generated on add fluent API.
25+
//modelBuilder.Entity<User>().Property(b => b.Id).ValueGeneratedOnAdd();
26+
}
27+
}
28+
29+
public class User
30+
{
31+
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
32+
public int Id { get; set; }
33+
34+
public string LastName { get; set; }
35+
36+
public string FirstName { get; set; }
37+
38+
public string DisplayName { get; set; }
39+
}
40+
}

0 commit comments

Comments
 (0)