Monday, March 27, 2017

EF Database Mapper

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Dummy.Model
{
    [Table("Items")]
    public class Item
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        [Required]
        public decimal Price { get; set; }
        [StringLength(500)]
        public string Description { get; set; }
        [Required]
        [StringLength(500)]
        public string Address { get; set; }

        public int CategoryId { get; set; }

        [ForeignKey("CategoryId")]
        public virtual Category Categories { get; set; }

        public virtual ICollection<OrderItem> OrderItems { get; set; }

        public Item()
        {
            OrderItems = new HashSet<OrderItem>();
        }
    }

    [Table("Categories")]
    public class Category
    {
        [Key]
        public int CategoryId { get; set; }
        [StringLength(50)]
        public string Name { get; set; }
        [StringLength(500)]
        public string Description { get; set; }
        public virtual ICollection<Item> Items { get; set; }

        public Category()
        {
            Items = new HashSet<Item>();
        }
    }
    [Table("OrderItems")]
    public class OrderItem
    {

        [Key]
        public int OrderItemId { get; set; }
        public int OrdersId { get; set; }
        public int ItemsId { get; set; }

        public int Quantity { get; set; }
        public decimal Price { get; set; }
        [ForeignKey("OrdersId")]
        public virtual Order Orders { get; set; }
        [ForeignKey("ItemsId")]
        public virtual Item Items { get; set; }

        public OrderItem()
        {
            Orders = new Order();
            Items = new Item();
        }
    }

    [Table("Orders")]
    public class Order
    {
        [Key]
        public int OrderId { get; set; }
        public DateTime Time { get; set; }

        public int CustomerId { get; set; }

        public virtual ICollection<OrderItem> OrderItems { get; set; }
        [ForeignKey("CustomerId")]
        public virtual Customer Customer { get; set; }

        public Order()
        {
            OrderItems = new HashSet<OrderItem>();
            Customer = new Customer();
        }
    }
    [Table("Customers")]
    public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }

        public DateTime DOB { get; set; }
        public virtual ICollection<Order> Orders { get; set; }

        public virtual CustomerContacts CustomerContacts { get; set; }
        public Customer()
        {
            Orders = new HashSet<Order>();
        }
    }

    [Table("CustomerContacts")]
    public class CustomerContacts
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int CustomerId { get; set; }
        [Required]
        [StringLength(50)]
        public string Phone { get; set; }
        [Required]
        [EmailAddress]
        [StringLength(50)]
        public string Email { get; set; }

        [Required]
        [StringLength(500)]
        public string Address { get; set; }

        [ForeignKey("CustomerId")]
        public virtual Customer Customers { get; set; }

        public CustomerContacts()
        {
        }
    }
}


///////////////
using Dummy.Model;
using System.Data.Entity;
namespace Dummy
{
    public class PlutoContext : DbContext
    {
        public PlutoContext() : base(@"Data Source=DELL\SQLEXPRESS;Initial Catalog=Dummey;Integrated Security=True")
        { }
        public DbSet<Item> Items { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<OrderItem> OrderItems { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<CustomerContacts> CustomerContacts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Customer>().
            HasOptional(e => e.CustomerContacts)
                    .WithRequired(e => e.Customers);
        }
    }
}

No comments:

Post a Comment

Sql server row level policy