Добавил модель Supplier

parent 37433489
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Store.Models;
namespace Store.Controllers
......@@ -20,16 +22,21 @@ namespace Store.Controllers
public IActionResult Details(string id)
{
Product product = _context.Products.FirstOrDefault(p => p.Id == id);
Product product = _context.Products
.Include(p => p.Supplier)
.First(p => p.Id.ToString() == id);
ViewBag.product = product;
ViewBag.supplier = product.Supplier;
return View();
}
[ActionName("CreateProduct")]
[HttpGet]
public IActionResult Create()
{
return View();
List<Supplier> suppliers = _context.Suppliers.ToList();
ViewBag.suppliers = suppliers;
return View(new Product());
}
[HttpPost]
......
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Store.Models;
namespace Store.Migrations
{
[DbContext(typeof(ProductContext))]
[Migration("20190605141259_AddSuppliersTable")]
partial class AddSuppliersTable
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("Store.Models.Product", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("Amount");
b.Property<string>("Brand");
b.Property<string>("Category");
b.Property<string>("Description");
b.Property<string>("Name");
b.Property<string>("SupplierId");
b.HasKey("Id");
b.HasIndex("SupplierId");
b.ToTable("Products");
});
modelBuilder.Entity("Store.Models.Supplier", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Supplier");
});
modelBuilder.Entity("Store.Models.Product", b =>
{
b.HasOne("Store.Models.Supplier", "Supplier")
.WithMany("Products")
.HasForeignKey("SupplierId");
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace Store.Migrations
{
public partial class AddSuppliersTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "SupplierId",
table: "Products",
nullable: true);
migrationBuilder.CreateTable(
name: "Suppliers",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Supplier", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Products_SupplierId",
table: "Products",
column: "SupplierId");
migrationBuilder.AddForeignKey(
name: "FK_Products_Supplier_SupplierId",
table: "Products",
column: "SupplierId",
principalTable: "Suppliers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Products_Supplier_SupplierId",
table: "Products");
migrationBuilder.DropTable(
name: "Suppliers");
migrationBuilder.DropIndex(
name: "IX_Products_SupplierId",
table: "Products");
migrationBuilder.DropColumn(
name: "SupplierId",
table: "Products");
}
}
}
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
......@@ -33,10 +34,33 @@ namespace Store.Migrations
b.Property<string>("Name");
b.Property<string>("SupplierId");
b.HasKey("Id");
b.HasIndex("SupplierId");
b.ToTable("Products");
});
modelBuilder.Entity("Store.Models.Supplier", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Suppliers");
});
modelBuilder.Entity("Store.Models.Product", b =>
{
b.HasOne("Store.Models.Supplier", "Supplier")
.WithMany("Products")
.HasForeignKey("SupplierId");
});
#pragma warning restore 612, 618
}
}
......
using System;
namespace Store.Models
{
public class Product
......@@ -8,5 +10,8 @@ namespace Store.Models
public string Brand { get; set; }
public int Amount { get; set; }
public string Description { get; set; }
public string SupplierId { get; set; }
public virtual Supplier Supplier { get; set; }
}
}
......@@ -5,6 +5,7 @@ namespace Store.Models
public class ProductContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public ProductContext(DbContextOptions<ProductContext> options) :
base(options)
......
using System.Collections.Generic;
namespace Store.Models
{
public class Supplier
{
public string Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
}
......@@ -29,7 +29,8 @@ namespace Store
});
string connection = Configuration.GetConnectionString("StoreConnection");
services.AddDbContext<ProductContext>(options => options.UseNpgsql(connection));
services.AddDbContext<ProductContext>(options => options
.UseNpgsql(connection));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
......
......@@ -6,7 +6,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.1.3" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.1.1" />
......
@using Store.Models
@model Store.Models.Product
@{
Layout = "_MainLayout";
......@@ -32,6 +33,16 @@
<input type="text" class="form-control" value="@Model.Description" id="Description" name="Description" placeholder="Enter amount">
</div>
<div class="form-group">
<label for="">Supplier</label>
<select name="SupplierId">
@foreach (Supplier s in ViewBag.suppliers)
{
<option value="@s.Id">@s.Name</option>
}
</select>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
......@@ -21,5 +21,9 @@
<td>Amount:</td>
<td>@ViewBag.product.Amount</td>
</tr>
<tr>
<td>Supplier Name:</td>
<td>@ViewBag.supplier.Name</td>
</tr>
</tbody>
</table>
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