Skip to content

Commit

Permalink
Basic implementation with EF Core
Browse files Browse the repository at this point in the history
  • Loading branch information
mderriey committed Feb 19, 2021
1 parent c3df01f commit 02a373d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
<PackageReference Include="Scrutor" Version="3.3.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/AzureIdentityLivestream.Web/Services/Person.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AzureIdentityLivestream.Web.Services
{
public record Person(string FirstName, string LastName, int Age);
public record Person(int Id, string FirstName, string LastName, int Age);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace AzureIdentityLivestream.Web.Services.Sql
{
public class EfCorePersonProvider : IPersonProvider
{
private readonly LivestreamContext _livestreamContext;

public EfCorePersonProvider(LivestreamContext livestreamContext)
{
_livestreamContext = livestreamContext;
}

public async Task<IEnumerable<Person>> GetPeople() => await _livestreamContext.People.ToListAsync();
}
}
22 changes: 22 additions & 0 deletions src/AzureIdentityLivestream.Web/Services/Sql/LivestreamContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;

namespace AzureIdentityLivestream.Web.Services.Sql
{
public class LivestreamContext : DbContext
{
public LivestreamContext([NotNull] DbContextOptions options) : base(options)
{
}

public DbSet<Person> People => Set<Person>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Person>()
.ToTable("Person")
.HasKey(x => x.Id);
}
}
}
11 changes: 4 additions & 7 deletions src/AzureIdentityLivestream.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AzureIdentityLivestream.Web.Services.Sql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -21,17 +22,13 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();

services.AddMemoryCache();
services.AddSingleton<IAzureSqlTokenProvider, AzureIdentityAzureSqlTokenProvider>();
services.Decorate<IAzureSqlTokenProvider, CacheAzureSqlTokenProvider>();

services.AddSingleton(provider =>
services.AddDbContext<LivestreamContext>(builder =>
{
var sqlConnectionString = Configuration.GetValue<string>("SqlConnectionString");
return new SqlConnectionFactory(sqlConnectionString, provider.GetRequiredService<IAzureSqlTokenProvider>());
builder.UseSqlServer(sqlConnectionString, options => options.EnableRetryOnFailure());
});

services.AddSingleton<IPersonProvider, DapperPersonProvider>();
services.AddScoped<IPersonProvider, EfCorePersonProvider>();

services.AddRazorPages();
}
Expand Down
2 changes: 1 addition & 1 deletion src/AzureIdentityLivestream.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"StorageConnectionString": "https://azureidentitylivestream.blob.core.windows.net",
"SqlConnectionString": "Data Source=tcp:azureidentitylivestream.database.windows.net,1433; Initial Catalog=azure-identity-livestream; Encrypt=True; TrustServerCertificate=False",
"SqlConnectionString": "Data Source=(localdb)\\azureidentitylivestream; Initial Catalog=azure-identity-livestream; Integrated Security=SSPI",
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down

0 comments on commit 02a373d

Please sign in to comment.