Commit e0eeec0e authored by SergeevaAA's avatar SergeevaAA

Merge branch 'feature/#2-AddDataAccessLibrary' into 'develop'

#2 - Реализована работа с БД

See merge request !5
parents afb559d0 74022c73
using System;
namespace FileDesk.DataAccess
{
public class Class1
{
}
}
......@@ -4,4 +4,22 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FileDesk.Domain\FileDesk.Domain.csproj" />
</ItemGroup>
</Project>
// <auto-generated />
using System;
using FileDesk.DataAccess.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace FileDesk.DataAccess.Migrations
{
[DbContext(typeof(FileDeskContext))]
[Migration("20201023144738_FilesAndDownloadsTablesCreate")]
partial class FilesAndDownloadsTablesCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("FileDesk.Domain.Models.Download", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<DateTime>("DateTime")
.HasColumnType("datetime2");
b.Property<long>("FileId")
.HasColumnType("bigint");
b.Property<string>("IpAddress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.HasIndex("FileId");
b.ToTable("Downloads");
});
modelBuilder.Entity("FileDesk.Domain.Models.File", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<byte[]>("Content")
.IsRequired()
.HasColumnType("varbinary(max)");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Format")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("UploadDateTime")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("Files");
});
modelBuilder.Entity("FileDesk.Domain.Models.Download", b =>
{
b.HasOne("FileDesk.Domain.Models.File", "File")
.WithMany("Downloads")
.HasForeignKey("FileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace FileDesk.DataAccess.Migrations
{
public partial class FilesAndDownloadsTablesCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Files",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: false),
Description = table.Column<string>(nullable: false),
UploadDateTime = table.Column<DateTime>(nullable: false),
Content = table.Column<byte[]>(nullable: false),
Format = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Files", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Downloads",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FileId = table.Column<long>(nullable: false),
DateTime = table.Column<DateTime>(nullable: false),
IpAddress = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Downloads", x => x.Id);
table.ForeignKey(
name: "FK_Downloads_Files_FileId",
column: x => x.FileId,
principalTable: "Files",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Downloads_FileId",
table: "Downloads",
column: "FileId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Downloads");
migrationBuilder.DropTable(
name: "Files");
}
}
}
\ No newline at end of file
// <auto-generated />
using System;
using FileDesk.DataAccess.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace FileDesk.DataAccess.Migrations
{
[DbContext(typeof(FileDeskContext))]
partial class FileDeskContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("FileDesk.Domain.Models.Download", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<DateTime>("DateTime")
.HasColumnType("datetime2");
b.Property<long>("FileId")
.HasColumnType("bigint");
b.Property<string>("IpAddress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.HasIndex("FileId");
b.ToTable("Downloads");
});
modelBuilder.Entity("FileDesk.Domain.Models.File", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<byte[]>("Content")
.IsRequired()
.HasColumnType("varbinary(max)");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Format")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("UploadDateTime")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("Files");
});
modelBuilder.Entity("FileDesk.Domain.Models.Download", b =>
{
b.HasOne("FileDesk.Domain.Models.File", "File")
.WithMany("Downloads")
.HasForeignKey("FileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
namespace FileDesk.DataAccess.Repository
{
using FileDesk.Domain.Models;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Reflection;
/// <summary>
/// Класс для работы с БД
/// </summary>
public class FileDeskContext : DbContext
{
/// <summary>
/// Создает новый экземпляр класса <see cref="FileDeskContext"/>
/// </summary>
public FileDeskContext() { }
/// <summary>
/// Создает новый экземпляр класса <see cref="FileDeskContext"/>
/// </summary>
/// <param name="options">Опции</param>
public FileDeskContext(DbContextOptions options) : base(options) { }
/// <summary>
/// Файлы
/// </summary>
public DbSet<File> Files { get; set; }
/// <summary>
/// Скачивания файлов
/// </summary>
public DbSet<Download> Downloads { get; set; }
/// <summary>
/// Метод при создании БД
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var entityTypes = modelBuilder.Model.GetEntityTypes().Select(t => t.ClrType).ToList();
foreach (var entityType in entityTypes)
{
var method = entityType.GetMethod("OnModelCreating", System.Reflection.BindingFlags.Static | BindingFlags.NonPublic);
method?.Invoke(null, new object[] { modelBuilder });
}
}
}
}
\ No newline at end of file
namespace FileDesk.Domain.Models
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
/// <summary>
......
namespace FileDesk.Domain.Models
{
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
/// <summary>
/// Скачивание файла
/// </summary>
public class Download
public class Download : AbstractEntity
{
/// <summary>
/// Идентификатор файла
/// </summary>
[Required]
public long FileId { get; set; }
/// <summary>
......@@ -20,11 +23,13 @@
/// <summary>
/// Дата и время скачивания
/// </summary>
[Required]
public DateTime DateTime { get; set; }
/// <summary>
/// IP-адрес компьютера, на который был скачан файл
/// </summary>
[Required]
public string IpAddress { get; set; }
}
}
\ No newline at end of file
......@@ -2,7 +2,9 @@
{
using FileDesk.Domain.Enums;
using System;
using System.Reflection.Metadata;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
/// <summary>
/// Файл
......@@ -12,26 +14,44 @@
/// <summary>
/// Название
/// </summary>
[Required]
public string Name { get; set; }
/// <summary>
/// Описание
/// </summary>
[Required]
public string Description { get; set; }
/// <summary>
/// Дата и время добавления
/// </summary>
[Required]
public DateTime UploadDateTime { get; set; }
/// <summary>
/// Сам файл
/// Содержимое файла
/// </summary>
public Blob Blob { get; set; }
[Required]
public byte[] Content { get; set; }
/// <summary>
/// Формат
/// </summary>
[Required]
public FileFormat Format { get; set; }
/// <summary>
/// Список скачиваний
/// </summary>
public List<Download> Downloads { get; set; }
/// <summary>
/// Создает новый экземпляр класса <see cref="File"/>
/// </summary>
public File()
{
Downloads = new List<Download>();
}
}
}
\ No newline at end of file
......@@ -4,4 +4,16 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FileDesk.DataAccess\FileDesk.DataAccess.csproj" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace FileDesk.Web
{
using FileDesk.DataAccess.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public class Startup
{
public Startup(IConfiguration configuration)
......@@ -23,6 +20,7 @@ namespace FileDesk.Web
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<FileDeskContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
}
......
{
"ConnectionStrings": {
"DefaultConnection": "Server=ANASTASIYA\\SQLEXPRESS;Database=FileDesk;Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment