Commit 6f377840 authored by Alexey Goikolov's avatar Alexey Goikolov

Добавил свойство IsDeleted в модель Phone. Создал миграцию. Поменял выборку из БД в экшене Index

parent de87b539
...@@ -22,7 +22,7 @@ namespace Lesson49.Controllers ...@@ -22,7 +22,7 @@ namespace Lesson49.Controllers
public IActionResult Index(string searchString) //Phones/Create public IActionResult Index(string searchString) //Phones/Create
{ {
List<Phone> phones = _db.Phones List<Phone> phones = _db.Phones
.Include(p => p.Brand) .Include(p => p.Brand).Where(p => p.IsDeleted == false)
.ToList(); .ToList();
if (!string.IsNullOrEmpty(searchString)) if (!string.IsNullOrEmpty(searchString))
phones = phones.Where(p => p.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)).ToList(); phones = phones.Where(p => p.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)).ToList();
...@@ -66,7 +66,8 @@ namespace Lesson49.Controllers ...@@ -66,7 +66,8 @@ namespace Lesson49.Controllers
Phone phone = _db.Phones.FirstOrDefault(p => p.Id == id); Phone phone = _db.Phones.FirstOrDefault(p => p.Id == id);
if (phone != null) if (phone != null)
{ {
_db.Phones.Remove(phone); phone.IsDeleted = true;
_db.Phones.Update(phone);
_db.SaveChanges(); _db.SaveChanges();
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
......
// <auto-generated />
using Lesson49.Models.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Lesson49.Migrations
{
[DbContext(typeof(MobileContext))]
[Migration("20210331141323_AddIsDeletedFieldInPhones")]
partial class AddIsDeletedFieldInPhones
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.13");
modelBuilder.Entity("Lesson49.Models.Brand", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("IsActive")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<string>("WebSite")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Brands");
});
modelBuilder.Entity("Lesson49.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Address")
.HasColumnType("TEXT");
b.Property<string>("ContactPhone")
.HasColumnType("TEXT");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<int>("PhoneId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("PhoneId");
b.ToTable("Orders");
});
modelBuilder.Entity("Lesson49.Models.Phone", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("BrandId")
.HasColumnType("INTEGER");
b.Property<bool>("IsDeleted")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<double>("Price")
.HasColumnType("REAL");
b.HasKey("Id");
b.HasIndex("BrandId");
b.ToTable("Phones");
});
modelBuilder.Entity("Lesson49.Models.Order", b =>
{
b.HasOne("Lesson49.Models.Phone", "Phone")
.WithMany()
.HasForeignKey("PhoneId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Lesson49.Models.Phone", b =>
{
b.HasOne("Lesson49.Models.Brand", "Brand")
.WithMany()
.HasForeignKey("BrandId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace Lesson49.Migrations
{
public partial class AddIsDeletedFieldInPhones : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "Phones",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsDeleted",
table: "Phones");
}
}
}
...@@ -69,6 +69,9 @@ namespace Lesson49.Migrations ...@@ -69,6 +69,9 @@ namespace Lesson49.Migrations
b.Property<int>("BrandId") b.Property<int>("BrandId")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<bool>("IsDeleted")
.HasColumnType("INTEGER");
b.Property<string>("Name") b.Property<string>("Name")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
......
...@@ -8,5 +8,7 @@ ...@@ -8,5 +8,7 @@
public int BrandId { get; set; } public int BrandId { get; set; }
public Brand Brand { get; set; } public Brand Brand { get; set; }
public double Price { get; set; } public double Price { get; set; }
public bool IsDeleted { get; set; }
} }
} }
\ No newline at end of file
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