Minimal API's در دات نت 6 - قسمت دوم - ایجاد مدلها و Context برنامه
نویسنده: وحید نصیری
تاریخ: ۱۴۰۰/۱۲/۲۴ ۱۰:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace MinimalBlog.Domain.Model;
public class Author
{
public int Id { get; set; }
public string FirstName { get; set; } = default!;
public string LastName { get; set; } = default!;
public string FullName => FirstName + " " + LastName;
public DateTime DateOfBirth { get; set; }
public string? Bio { get; set; }
} namespace MinimalBlog.Domain.Model;
public class Article
{
public Article()
{
Categories = new List<Category>();
}
public int Id { get; set; }
public string Title { get; set; } = default!;
public string? Subtitle { get; set; }
public string Body { get; set; } = default!;
public int AuthorId { get; set; }
public Author Author { get; set; } = default!;
public DateTime DateCreated { get; set; }
public DateTime LastUpdated { get; set; }
public int NumberOfLikes { get; set; }
public int NumberOfShares { get; set; }
public ICollection<Category> Categories { get; set; }
} namespace MinimalBlog.Domain.Model;
public class Blog
{
public Blog()
{
Contributors = new List<Author>();
}
public int Id { get; set; }
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
public DateTime CreatedDate { get; set; }
public int AuthorId { get; set; }
public Author Owner { get; set; } = default!;
public ICollection<Author> Contributors { get; }
} namespace MinimalBlog.Domain.Model;
public class Category
{
public Category()
{
Articles = new List<Article>();
}
public int Id { get; set; }
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
public ICollection<Article> Articles { get; set; }
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MinimalBlog.Domain\MinimalBlog.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project> using Microsoft.EntityFrameworkCore;
using MinimalBlog.Domain.Model;
namespace MinimalBlog.Dal;
public class MinimalBlogDbContext : DbContext
{
public MinimalBlogDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Article> Articles { get; set; } = default!;
public DbSet<Category> Categories { get; set; } = default!;
public DbSet<Author> Authors { get; set; } = default!;
public DbSet<Blog> Blogs { get; set; } = default!;
} {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Default": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MinimalBlog;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
} <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MinimalBlog.Domain\MinimalBlog.Domain.csproj" />
<ProjectReference Include="..\MinimalBlog.Dal\MinimalBlog.Dal.csproj" />
</ItemGroup>
</Project> using Microsoft.EntityFrameworkCore;
using MinimalBlog.Dal;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var connectionString = builder.Configuration.GetConnectionString("Default");
builder.Services.AddDbContext<MinimalBlogDbContext>(opt => opt.UseSqlServer(connectionString)); For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%a_%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME: =0%") do (set mytime=%%a%%b)
dotnet tool update --global dotnet-ef --version 6.0.2
dotnet build
dotnet ef migrations --startup-project ../MinimalBlog.Api/ add V%mydate%_%mytime% --context MinimalBlogDbContext
pause [*.cs] generated_code = true
dotnet tool update --global dotnet-ef --version 6.0.2 dotnet build dotnet ef --startup-project ../MinimalBlog.Api/ database update --context MinimalBlogDbContext pause
namespace MinimalBlog.Dal.SqlServer
public class SqlServerDbContext : MinimalBlogDbContext
{
//Sql Server Settings
} namespace MinimalBlog.Dal.Sqlite
public class SqliteDbContext : MinimalBlogDbContext
{
//Sqlite Settings
} var connectionStringKey = Configuration.GetConnectionString("InUseKey");
var connectionString = Configuration.GetConnectionString(connectionStringKey);
switch (connectionStringKey)
{
case "SqlServerConnection":
services.AddScoped<MinimalBlogDbContext, SqlServerDbContext>();
services.AddDbContext<SqlServerDbContext>(options => opt.UseSqlServer(connectionString));
break;
case "SqliteConnection":
services.AddScoped<MinimalBlogDbContext, SqliteDbContext>();
services.AddDbContext<SqliteDbContext>(options => options.UseSqlite(connectionString));
break;
default:
throw new NotImplementedException($"`{connectionStringKey}` is not defined in `appsettings.json` file.");
}