Project

Profile

Help

HostedRedmine.com has moved to the Planio platform. All logins and passwords remained the same. All users will be able to login and use Redmine just as before. "Read more...":https://support.plan.io/news/187

Bug #502882 » Program.cs

Mykhailo Voronovskii, 2015-12-17 06:27 PM

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Validation;

namespace Laba5
{
class Program
{
static string choise, spreadsheet, firstP, secondP, thirdP;

static void Main(string[] args)
{
using (var db = new DBLaba5())
{
do
{
Console.WriteLine("\n\t Menu:\n");
Console.WriteLine("\t1.Print DB\n\t2.Insert item\n\t3.Delete item");
Console.WriteLine("\t4.Modify item\n\t5.Exit\n");
Console.Write("Enter key:");
choise = Console.ReadLine();
switch (choise)
{
case "1":
PrintDB();
break;

case "2":
InsertItem();
break;

case "3":
DeleteItem();
break;

case "4":
ModifyItem();
break;

case "5":
Console.Write("Exiting...");
break;

default:
Console.Write("Wrong input.");
break;
}
}
while (choise != "5");
}
}

public static void PrintDB()
{
using (var db = new DBLaba5())
{
Console.WriteLine("\n\n\n\t Spreadsheets:\n");
Console.WriteLine("\t1.Vycladach\n\t2.Kafedra\n\t3.Instytut");
Console.Write("Enter key:");
spreadsheet = Console.ReadLine();
switch (spreadsheet)
{
case "1":
Console.WriteLine("\n\n----------------");
Console.WriteLine("|P.I.\tSubject|");
Console.WriteLine("----------------");
foreach (Vycladach vyc in db.Vycladaches)
{
Console.WriteLine(string.Format("{0}\t{1}", vyc.P_I_, vyc.Predmet));
}

break;

case "2":
/*Console.WriteLine("\n\n-----------------");
Console.WriteLine("|Name\tCabinet|");
Console.WriteLine("-----------------");

foreach (Kafedra kaf in db.Kafedra)
{
Console.WriteLine(string.Format("{0}\t{1}", kaf.Nazva, kaf.Corpus));
}*/

Console.WriteLine("\n\n------------------------");
Console.WriteLine("|Name\tCabinet\tDirector|");
Console.WriteLine("------------------------");

foreach (Kafedra kaf in db.Kafedras)
{
Console.WriteLine(string.Format("{0}\t{1}\t{2}", kaf.Nazva, kaf.Corpus, kaf.Director));
}

break;

case "3":
Console.WriteLine("\n\n------------------");
Console.WriteLine("|Name\tContacts|");
Console.WriteLine("------------------");
foreach (Instytut ins in db.Instytuts)
{
Console.WriteLine(string.Format("{0}\t{1}", ins.Nazva, ins.Contacty));
}

break;

default:
Console.Write("\n\nWrong input.\n\n");
break;
}
}
}

public static void InsertItem()
{
using (var db = new DBLaba5())
{
Console.WriteLine("\n\n\n\t Spreadsheets:\n");
Console.WriteLine("\t1.Vycladach\n\t2.Kafedra\n\t3.Instytut");
Console.Write("Enter key:");
spreadsheet = Console.ReadLine();
switch (spreadsheet)
{
case "1":
Console.Write("P.I.: ");
firstP = Console.ReadLine();
Console.Write("Subject: ");
secondP = Console.ReadLine();

var newVyc = new Vycladach { P_I_ = firstP, Predmet = secondP};
db.Vycladaches.Add(newVyc);

Validation(db);
break;

case "2":
Console.Write("Name: ");
firstP = Console.ReadLine();
Console.Write("Cabinet: ");
secondP = Console.ReadLine();

Console.Write("Director: ");
thirdP = Console.ReadLine();

var newKaf = new Kafedra { Nazva = firstP, Corpus = secondP, Director = thirdP };
db.Kafedras.Add(newKaf);

Validation(db);
break;

case "3":
Console.Write("Name: ");
firstP = Console.ReadLine();
Console.Write("Contacts: ");
secondP = Console.ReadLine();

var newIns = new Instytut { Nazva = firstP, Contacty = secondP };
db.Instytuts.Add(newIns);

Validation(db);
break;

default:
Console.Write("\n\nWrong input.\n\n");
break;
}
}
}

public static void DeleteItem()
{
using (var db = new DBLaba5())
{
Console.WriteLine("\n\n\n\t Spreadsheets:\n");
Console.WriteLine("\t1.Vycladach\n\t2.Kafedra\n\t3.Instytut");
Console.Write("Enter key:");
spreadsheet = Console.ReadLine();
switch (spreadsheet)
{
case "1":
Console.Write("P.I.: ");
firstP = Console.ReadLine();
var vyc = new Vycladach() { P_I_ = firstP };
db.Vycladaches.Attach(vyc);
db.Vycladaches.Remove(vyc);

Validation(db);
break;

case "2":
Console.Write("Name: ");
firstP = Console.ReadLine();
var kaf = new Kafedra() { Nazva = firstP };
db.Kafedras.Attach(kaf);
db.Kafedras.Remove(kaf);

Validation(db);
break;

case "3":
Console.Write("Name: ");
firstP = Console.ReadLine();
var ins = new Instytut() { Nazva = firstP };
db.Instytuts.Attach(ins);
db.Instytuts.Remove(ins);

Validation(db);
break;

default:
Console.Write("\n\nWrong input.\n\n");
break;
}
}
}

public static void ModifyItem()
{
using (var db = new DBLaba5())
{
Console.WriteLine("\n\n\n\t Spreadsheets:\n");
Console.WriteLine("\t1.Vycladach\n\t2.Kafedra\n\t3.Instytut");
Console.Write("Enter key:");
spreadsheet = Console.ReadLine();
switch (spreadsheet)
{
case "1":
Console.Write("P.I.: ");
firstP = Console.ReadLine();
var vyc = new Vycladach() { P_I_ = firstP };
Console.Write("New Subject: ");
secondP = Console.ReadLine();

db.Vycladaches.Attach(vyc).Predmet = secondP;

string value = db.Entry(vyc).Property(m => m.Predmet).OriginalValue;
Console.WriteLine(string.Format("Original Value : {0}", value));

value = db.Entry(vyc).Property(m => m.Predmet).CurrentValue;
Console.WriteLine(string.Format("Current Value : {0}", value));

value = db.Entry(vyc).GetDatabaseValues().GetValue<string>("Predmet");
Console.WriteLine(string.Format("DB Value : {0}", value));


Validation(db);
break;

case "2":
Console.Write("Name: ");
firstP = Console.ReadLine();
var kaf = new Kafedra() { Nazva = firstP };
Console.Write("New Cabinet: ");
secondP = Console.ReadLine();

Console.Write("New Director: ");
thirdP = Console.ReadLine();

db.Kafedras.Attach(kaf).Corpus = secondP;
db.Kafedras.Attach(kaf).Director = thirdP;

value = db.Entry(kaf).Property(m => m.Corpus).OriginalValue;
Console.WriteLine(string.Format("Original Value : {0}", value));
value = db.Entry(kaf).Property(m => m.Director).OriginalValue;
Console.WriteLine(string.Format("Original Value : {0}", value));

value = db.Entry(kaf).Property(m => m.Corpus).CurrentValue;
Console.WriteLine(string.Format("Current Value : {0}", value));
value = db.Entry(kaf).Property(m => m.Director).CurrentValue;
Console.WriteLine(string.Format("Current Value : {0}", value));

value = db.Entry(kaf).GetDatabaseValues().GetValue<string>("Corpus");
Console.WriteLine(string.Format("DB Value : {0}", value));
value = db.Entry(kaf).GetDatabaseValues().GetValue<string>("Director");
Console.WriteLine(string.Format("DB Value : {0}", value));

Validation(db);
break;

case "3":
Console.Write("Name: ");
firstP = Console.ReadLine();
var ins = new Instytut() { Nazva = firstP };
Console.Write("New Contacts: ");
secondP = Console.ReadLine();

db.Instytuts.Attach(ins).Contacty = secondP;

value = db.Entry(ins).Property(m => m.Contacty).OriginalValue;
Console.WriteLine(string.Format("Original Value : {0}", value));

value = db.Entry(ins).Property(m => m.Contacty).CurrentValue;
Console.WriteLine(string.Format("Current Value : {0}", value));

value = db.Entry(ins).GetDatabaseValues().GetValue<string>("Contacty");
Console.WriteLine(string.Format("DB Value : {0}", value));

Validation(db);
break;

default:
Console.Write("\n\nWrong input.\n\n");
break;
}
}
}

public static void Validation(DBLaba5 db)
{
try
{
db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var validationErrors = db.GetValidationErrors().Where(vr => !vr.IsValid).SelectMany(vr => vr.ValidationErrors);

foreach (var error in validationErrors)
{
Console.WriteLine(error.ErrorMessage);
}

Console.ReadKey();
}
}
}
}
    (1-1/1)