Thursday 29 September 2016

c# entity frame 2

using inventory_app;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dframe
{
    public class supplier_manager
    {
//pass small section of data from database to client
        public static List<supplier_dto> get_all()
        {
            var db = new InventoryEntities();
            var suppliers = db.Suppliers.Select(s=>new supplier_dto { id = s.ID, name = s.Name }).ToList();
            return suppliers;
        }

        public static void add(supplier_dto s)
        {
            var db = new InventoryEntities();

            var supp = new Supplier { Name = s.name };
            db.Suppliers.Add(supp);
            db.SaveChanges();
        }

        public static void update(supplier_dto s)
        {
            var db = new InventoryEntities();
            var supp = db.Suppliers.SingleOrDefault(a => a.ID == s.id);
            supp.Name = s.name;
            db.SaveChanges();
        }

        public static supplier_dto find(int id)
        {
            var db = new InventoryEntities();
            var supp = db.Suppliers.Find(id);
            var supplier = new supplier_dto { id = supp.ID, name = supp.Name };
            return supplier;
        }
    }
}

-------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace inventory_app
{
    public class supplier_dto
    {
        public int id { get; set; }
        public string name { get; set; }
    }
}
-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace dframe
{
    public class product_manager
    {
        public static IList get_all()
        {
            var db = new InventoryEntities();
            var prods = db.Products.ToList();
            return prods;
        }

        public static IList get_product(int supplier_id)
        {
            var db = new InventoryEntities();
            var prods = db.Products.Where(p => p.SupplierID == supplier_id).
                Select(p => new { Product = p.Name, p.Price, p.Quantity }).ToList();
            return prods;
        }
    }
}

No comments:

Post a Comment