Skip to content

Commit 57e9ba4

Browse files
committed
apply chnages in program.cs
1 parent f5bc307 commit 57e9ba4

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

NorthwindCRUD/Models/Contracts/IOrder.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using NorthwindCRUD.Models.Dtos;
2-
using NorthwindCRUD.Models.Enums;
1+
using NorthwindCRUD.Models.Dtos;
2+
using static NorthwindCRUD.Helpers.Enums;
33

44
namespace NorthwindCRUD.Models.Contracts
55
{
@@ -11,7 +11,7 @@ public interface IOrder
1111

1212
int EmployeeId { get; set; }
1313

14-
int? ShipperId { get; set; }
14+
Shipping? ShipperId { get; set; }
1515

1616
string OrderDate { get; set; }
1717

@@ -26,8 +26,6 @@ public interface IOrder
2626

2727
public bool Completed { get; set; }
2828

29-
public bool Completed { get; set; }
30-
3129
AddressDto ShipAddress { get; set; }
3230
}
3331
}

NorthwindCRUD/Program.cs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using AutoMapper;
33
using GraphQL.AspNet.Configuration.Mvc;
44
using Microsoft.AspNetCore.Authentication.JwtBearer;
5+
using Microsoft.Data.Sqlite;
6+
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.EntityFrameworkCore.Diagnostics;
8+
using Microsoft.Extensions.Options;
59
using Microsoft.IdentityModel.Tokens;
610
using Microsoft.OpenApi.Models;
7-
using Newtonsoft.Json;
811
using Newtonsoft.Json.Converters;
912
using NorthwindCRUD.Filters;
1013
using NorthwindCRUD.Helpers;
11-
using NorthwindCRUD.Middlewares;
12-
using NorthwindCRUD.Providers;
1314
using NorthwindCRUD.Services;
1415

1516
namespace NorthwindCRUD
@@ -37,8 +38,7 @@ public static void Main(string[] args)
3738
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
3839
}).AddNewtonsoftJson(options =>
3940
{
40-
options.SerializerSettings.DateParseHandling = DateParseHandling.None;
41-
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
41+
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
4242
options.SerializerSettings.Converters.Add(new StringEnumConverter());
4343
});
4444

@@ -57,7 +57,6 @@ public static void Main(string[] args)
5757
Scheme = "bearer",
5858
});
5959

60-
option.SchemaFilter<EnumSchemaFilter>();
6160
option.SchemaFilter<EnumSchemaFilter>();
6261
option.OperationFilter<AuthResponsesOperationFilter>();
6362
option.EnableAnnotations();
@@ -75,10 +74,36 @@ public static void Main(string[] args)
7574
});
7675
});
7776

78-
builder.Services.AddDbContext<DataContext>((serviceProvider, options) =>
77+
var dbProvider = builder.Configuration.GetConnectionString("Provider");
78+
79+
if (dbProvider == "SQLite")
7980
{
80-
var configurationProvider = serviceProvider.GetRequiredService<DbContextConfigurationProvider>();
81-
configurationProvider.ConfigureOptions(options);
81+
// For SQLite in memory to be shared across multiple EF calls, we need to maintain a separate open connection.
82+
// see post https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
83+
var keepAliveConnection = new SqliteConnection(builder.Configuration.GetConnectionString("SQLiteConnectionString"));
84+
keepAliveConnection.Open();
85+
}
86+
87+
builder.Services.AddDbContext<DataContext>(options =>
88+
{
89+
if (dbProvider == "SqlServer")
90+
{
91+
options.UseSqlServer(builder.Configuration.GetConnectionString("SqlServerConnectionString"));
92+
}
93+
else if (dbProvider == "InMemory")
94+
{
95+
options.ConfigureWarnings(warnOpts =>
96+
{
97+
// InMemory doesn't support transactions and we're ok with it
98+
warnOpts.Ignore(InMemoryEventId.TransactionIgnoredWarning);
99+
});
100+
101+
options.UseInMemoryDatabase(databaseName: builder.Configuration.GetConnectionString("InMemoryDBConnectionString"));
102+
}
103+
else if (dbProvider == "SQLite")
104+
{
105+
options.UseSqlite(builder.Configuration.GetConnectionString("SQLiteConnectionString"));
106+
}
82107
});
83108

84109
var config = new MapperConfiguration(cfg =>
@@ -110,10 +135,8 @@ public static void Main(string[] args)
110135
});
111136

112137
builder.Services.AddAuthorization();
113-
builder.Services.AddHttpContextAccessor();
114-
builder.Services.AddMemoryCache();
138+
115139
builder.Services.AddScoped<DBSeeder>();
116-
builder.Services.AddScoped<DbContextConfigurationProvider>();
117140
builder.Services.AddTransient<CategoryService>();
118141
builder.Services.AddTransient<CustomerService>();
119142
builder.Services.AddTransient<EmployeeTerritoryService>();
@@ -132,7 +155,7 @@ public static void Main(string[] args)
132155

133156
// Necessary to detect if it's behind a load balancer, for example changing protocol, port or hostname
134157
app.UseForwardedHeaders();
135-
app.UseMiddleware<TenantHeaderValidationMiddleware>();
158+
136159
app.UseHttpsRedirection();
137160
app.UseDefaultFiles();
138161
app.UseStaticFiles();
@@ -163,4 +186,4 @@ public static void Main(string[] args)
163186
app.Run();
164187
}
165188
}
166-
}
189+
}

0 commit comments

Comments
 (0)