DevSampleExam/Models/dbcontext.cs
2022-02-01 21:40:17 +01:00

59 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace SimpleModelsAndRelations.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public List<Order_Product> Order_Product { get; set; }
}
// TODO 3: complete the implementation below (1 pts)
public class Order_Product
{
public int Quantity { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public List<Order_Product> Order_Product { get; set; }
}
public partial class SimpleModelsAndRelationsContext : DbContext
{
// TODO 1: complete the implementation below (1 pt)
public DbSet<User> Users { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Order_Product> Order_Products { get; set; }
public SimpleModelsAndRelationsContext(DbContextOptions<SimpleModelsAndRelationsContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// TODO 2: complete the implementation below (0.5 pts)
modelBuilder.Entity<Order_Product>().HasKey(_op => new { _op.ProductId, _op.OrderId });
}
}
}