10.3 Defining EF Core models -> Building an EF Core modal for the Northwinid tables
parent
8945220f80
commit
4e99e8c92b
|
@ -0,0 +1,20 @@
|
|||
using System.ComponentModel.DataAnnotations.Schema; // [Column]
|
||||
|
||||
namespace Packt.Shared;
|
||||
public class Category
|
||||
{
|
||||
// these properties map to columns in the database
|
||||
public int CategoryId { get; set; }
|
||||
public string? CategoryName { get; set; }
|
||||
[Column(TypeName = "ntext")]
|
||||
public string? Description { get; set; }
|
||||
// defines a navigation property for related rows (one to many relationship with Products)
|
||||
public virtual ICollection<Product> Products { get; set; }
|
||||
|
||||
public Category()
|
||||
{
|
||||
// to enable developers to add products to a Category we must
|
||||
// initialize the navigation property to an empty collection
|
||||
Products = new HashSet<Product>();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Packt.Shared;
|
||||
|
||||
public class Product
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
[Required] [StringLength(40)] public string ProductName { get; set; } = null!;
|
||||
[Column("UnitPrice", TypeName = "money")]
|
||||
public decimal? Cost { get; set; }
|
||||
[Column("UnitsInStock")]
|
||||
public short? Stock { get; set; }
|
||||
public bool Discontinued { get; set; }
|
||||
// these two define the foreign key relationship
|
||||
// to the Categories table
|
||||
public int CategoryId { get; set; }
|
||||
public virtual Category Category { get; set; } = null;
|
||||
}
|
Loading…
Reference in New Issue