Skip to content

Commit ffc761e

Browse files
author
raihanM95
committed
Dependency injection for connection string & Install necessary packages
1 parent e9bf148 commit ffc761e

File tree

8 files changed

+201
-2
lines changed

8 files changed

+201
-2
lines changed

Domain/Domain.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8+
<PlatformTarget>AnyCPU</PlatformTarget>
9+
<WarningLevel>5</WarningLevel>
10+
</PropertyGroup>
11+
712
</Project>

Infrastructure.Persistence/Infrastructure.Persistence.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
8+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.11" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
914
</ItemGroup>
1015

1116
<ItemGroup>

Infrastructure.Persistence/Migrations/20210118081032_Initial.Designer.cs

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
namespace Infrastructure.Persistence.Migrations
5+
{
6+
public partial class Initial : Migration
7+
{
8+
protected override void Up(MigrationBuilder migrationBuilder)
9+
{
10+
migrationBuilder.CreateTable(
11+
name: "Products",
12+
columns: table => new
13+
{
14+
Id = table.Column<int>(nullable: false)
15+
.Annotation("SqlServer:Identity", "1, 1"),
16+
CreatedBy = table.Column<string>(nullable: true),
17+
Created = table.Column<DateTime>(nullable: false),
18+
LastModifiedBy = table.Column<string>(nullable: true),
19+
LastModified = table.Column<DateTime>(nullable: true),
20+
Name = table.Column<string>(nullable: true),
21+
Barcode = table.Column<string>(nullable: true),
22+
Description = table.Column<string>(nullable: true)
23+
},
24+
constraints: table =>
25+
{
26+
table.PrimaryKey("PK_Products", x => x.Id);
27+
});
28+
}
29+
30+
protected override void Down(MigrationBuilder migrationBuilder)
31+
{
32+
migrationBuilder.DropTable(
33+
name: "Products");
34+
}
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// <auto-generated />
2+
using System;
3+
using Infrastructure.Persistence.Contexts;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Infrastructure;
6+
using Microsoft.EntityFrameworkCore.Metadata;
7+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
8+
9+
namespace Infrastructure.Persistence.Migrations
10+
{
11+
[DbContext(typeof(RepositoryContext))]
12+
partial class RepositoryContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder
18+
.HasAnnotation("ProductVersion", "3.1.11")
19+
.HasAnnotation("Relational:MaxIdentifierLength", 128)
20+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
21+
22+
modelBuilder.Entity("Domain.Entities.Product", b =>
23+
{
24+
b.Property<int>("Id")
25+
.ValueGeneratedOnAdd()
26+
.HasColumnType("int")
27+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
28+
29+
b.Property<string>("Barcode")
30+
.HasColumnType("nvarchar(max)");
31+
32+
b.Property<DateTime>("Created")
33+
.HasColumnType("datetime2");
34+
35+
b.Property<string>("CreatedBy")
36+
.HasColumnType("nvarchar(max)");
37+
38+
b.Property<string>("Description")
39+
.HasColumnType("nvarchar(max)");
40+
41+
b.Property<DateTime?>("LastModified")
42+
.HasColumnType("datetime2");
43+
44+
b.Property<string>("LastModifiedBy")
45+
.HasColumnType("nvarchar(max)");
46+
47+
b.Property<string>("Name")
48+
.HasColumnType("nvarchar(max)");
49+
50+
b.HasKey("Id");
51+
52+
b.ToTable("Products");
53+
});
54+
#pragma warning restore 612, 618
55+
}
56+
}
57+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Infrastructure.Persistence.Contexts;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace Web.Extensions
11+
{
12+
public static class ServiceExtensions
13+
{
14+
public static void ConfigureSqlContext(this IServiceCollection services, IConfiguration config)
15+
{
16+
var connectionString = config["ConnectionStrings:DefaultDBConnection"];
17+
services.AddDbContext<RepositoryContext>(option => option.UseSqlServer(connectionString));
18+
}
19+
20+
21+
}
22+
}

Web/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Linq;
1010
using System.Threading.Tasks;
11+
using Web.Extensions;
1112

1213
namespace Web
1314
{
@@ -24,6 +25,8 @@ public Startup(IConfiguration configuration)
2425
public void ConfigureServices(IServiceCollection services)
2526
{
2627
services.AddControllersWithViews();
28+
29+
services.ConfigureSqlContext(Configuration);
2730
}
2831

2932
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Web/Web.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.11">
9+
<PrivateAssets>all</PrivateAssets>
10+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
11+
</PackageReference>
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Infrastructure.Persistence\Infrastructure.Persistence.csproj" />
17+
</ItemGroup>
18+
719
</Project>

0 commit comments

Comments
 (0)