Реализовал подключение к БД и модели
This commit is contained in:
parent
f660eb0d47
commit
1814173da3
19
InternetShop/InternetShopDatabase/InternetShopDatabase.cs
Normal file
19
InternetShop/InternetShopDatabase/InternetShopDatabase.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -6,4 +6,18 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</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>
|
</Project>
|
||||||
|
45
InternetShop/InternetShopDatabase/Models/Order.cs
Normal file
45
InternetShop/InternetShopDatabase/Models/Order.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
InternetShop/InternetShopDatabase/Models/Product.cs
Normal file
31
InternetShop/InternetShopDatabase/Models/Product.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user