Реализовал подключение к БД и модели

This commit is contained in:
Никита Потапов 2024-11-20 11:53:03 +04:00
parent f660eb0d47
commit 1814173da3
4 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,19 @@
using InternetShopDatabase.Models;
using Microsoft.EntityFrameworkCore;
namespace InternetShopDatabase
{
public class InternetShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost;User Id=SA;Password=87cbn9y48392nu32;Initial Catalog=InternetShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Product> Products { set; get; }
}
}

View File

@ -6,4 +6,18 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InternetShopContracts\InternetShopContracts.csproj" />
<ProjectReference Include="..\InternetShopDataModels\InternetShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,45 @@
using InternetShopContracts.DataBindingModels;
using InternetShopContracts.DataViewModels;
using InternetShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace InternetShopDatabase.Models
{
public class Order : IOrderModel
{
[Required]
public string CustomerFIO { get; set; } = string.Empty;
[Required]
public string CustomerEmail { get; set; } = string.Empty;
[Required]
public string ImagePath { get; set; } = string.Empty;
public List<string> ProductNames { get; set; } = new List<string>();
public int Id { get; set; }
public OrderViewModel GetViewModel => new()
{
Id = Id,
CustomerFIO = CustomerFIO,
CustomerEmail = CustomerEmail,
ImagePath = ImagePath,
ProductNames = ProductNames,
};
public static Order? Create(OrderBindingModel model)
{
return new Order()
{
Id = model.Id,
CustomerFIO = model.CustomerFIO,
CustomerEmail = model.CustomerEmail,
ImagePath = model.ImagePath,
ProductNames = model.ProductNames,
};
}
public void Update(OrderBindingModel model)
{
CustomerFIO = model.CustomerFIO;
CustomerEmail = model.CustomerEmail;
ImagePath = model.ImagePath;
ProductNames = model.ProductNames;
}
}
}

View File

@ -0,0 +1,31 @@
using InternetShopContracts.DataBindingModels;
using InternetShopContracts.DataViewModels;
using InternetShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace InternetShopDatabase.Models
{
public class Product : IProductModel
{
[Required]
public string Name { get; set; } = string.Empty;
public int Id { get; set; }
public ProductViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
};
public static Product? Create(ProductBindingModel model)
{
return new Product()
{
Id = model.Id,
Name = model.Name,
};
}
public void Update(ProductBindingModel model)
{
Name = model.Name;
}
}
}