Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DAO
- {
- static void Main()
- {
- Edit("ALFKI", "Plamen");
- Add("GRIVICA", "T233I");
- }
- static void Add(string name, string id)
- {
- Customer newCustomer = new Customer()
- {
- CompanyName = name,
- CustomerID = id
- };
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- bool isInDB = IsInDataBase(db, id);
- if (!isInDB)
- {
- db.Customers.Add(newCustomer);
- db.SaveChanges();
- Console.WriteLine("Added Successful.");
- }
- else
- {
- throw new ArgumentException("Such customer already exists");
- }
- }
- }
- static void Edit(string id, string newContactName)
- {
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
- customer.ContactName = newContactName;
- db.SaveChanges();
- }
- }
- static void Delete(string id)
- {
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
- db.Customers.Remove(customer);
- db.SaveChanges();
- }
- }
- static bool IsInDataBase(NORTHWNDEntities db, string id)
- {
- bool alreadyInDB = db.Customers.Where(a => a.CustomerID == id).Any();
- return alreadyInDB;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement