Skip to content

Commit ebf698b

Browse files
committed
added ef tooling support
1 parent 891fadd commit ebf698b

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

api/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public override void Configure(IFunctionsHostBuilder builder)
1616
builder.Services.AddDbContext<TodoContext>(
1717
options => options.UseSqlServer(connectionString)
1818
);
19-
}
20-
19+
}
2120
}
2221
}

api/ToDoHandlerEFCore.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
using System.Data;
1414
using Microsoft.Data.SqlClient;
1515
using Microsoft.EntityFrameworkCore;
16+
using Microsoft.EntityFrameworkCore.Design;
1617
using System.ComponentModel.DataAnnotations.Schema;
17-
using Dapper;
1818

1919
namespace Todo.Backend.EFCore
2020
{
@@ -36,6 +36,7 @@ public class TodoContext : DbContext
3636
public TodoContext(DbContextOptions<TodoContext> options)
3737
: base(options)
3838
{ }
39+
3940
protected override void OnModelCreating(ModelBuilder modelBuilder)
4041
{
4142
modelBuilder.HasSequence<int>("global_sequence");
@@ -44,15 +45,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4445
.Property(o => o.Id)
4546
.HasDefaultValueSql("NEXT VALUE FOR global_sequence");
4647
}
48+
4749
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
4850
=> optionsBuilder
4951
.LogTo(Console.WriteLine, LogLevel.Information)
5052
.EnableSensitiveDataLogging()
5153
.EnableDetailedErrors();
52-
54+
5355
public DbSet<Todo> Todos { get; set; }
5456
}
5557

58+
public class TodoContextFactory: IDesignTimeDbContextFactory<TodoContext>
59+
{
60+
public TodoContext CreateDbContext(string[] args)
61+
{
62+
var optionsBuilder = new DbContextOptionsBuilder<TodoContext>();
63+
optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable("AzureSQL"));
64+
65+
return new TodoContext(optionsBuilder.Options);
66+
}
67+
}
68+
5669
public class ToDoHandler
5770
{
5871
private TodoContext _todoContext;

api/api.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<PackageReference Include="Dapper" Version="2.0.123" />
88
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
99
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.10">
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
<PrivateAssets>all</PrivateAssets>
13+
</PackageReference>
1014
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
1115
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
1216
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />

0 commit comments

Comments
 (0)