private void button1_Click(object sender, EventArgs e) { AddToLog("Starting data copy."); AdventureWorks2016CTP3Entities adventureContext = new AdventureWorks2016CTP3Entities(); NorthwindEntities northwindContext = new NorthwindEntities(); int rows = 0; foreach (Database.Northwind.Customers nwCust in northwindContext.Customers) { string contact = nwCust.ContactName; string[] nameParts = contact.Split(' '); Database.Adventure.Person person = new Person() { FirstName = nameParts[0], LastName = nameParts[1], // PersonPhone = nwCust.Phone // ... }; AddToLog($"Processing customer: {nwCust.CompanyName}..."); adventureContext.Person.Add(person); rows++; if ((rows % 1000) == 0) { //adventureContext.SaveChanges(); } } //adventureContext.SaveChanges(); AddToLog($"Data copy complete, {rows} rows copied."); }
// normally we don't want to insert dummy records to the real databasae public static Person AddAPerson(string name, AdventureWorks2016CTP3Entities dbContext) { var newPersonID = GetPeopleCount(dbContext) + 1; var newPerson = dbContext.People.Add(new Person { PersonID = newPersonID, Name = name }); dbContext.SaveChanges(); return(newPerson); }
static void Main(string[] args) { var dbContext = new AdventureWorks2016CTP3Entities(); // let's print name int personID = 1; string name = GetPersonName(personID, dbContext); Console.WriteLine("Name = " + name); Console.WriteLine("Count = " + GetPeopleCount(dbContext)); Console.WriteLine("New test person added, with new ID = " + AddAPerson("Test", dbContext).PersonID); Console.WriteLine("Done. Please press any key.."); Console.ReadKey(); }
private void button2_Click(object sender, EventArgs e) { AdventureWorks2016CTP3Entities adventureContext = new AdventureWorks2016CTP3Entities(); Vendor vendor = new Vendor { Name = "Uusi yritys Oy" }; adventureContext.Vendor.Add(vendor); adventureContext.SaveChanges(); /* * Store newestStore = (from s in adventureContext.Store * orderby s.BusinessEntityID descending * select s).FirstOrDefault(); */ MessageBox.Show("Vendor ID = " + vendor.BusinessEntityID); }
public static int GetPeopleCount(AdventureWorks2016CTP3Entities dbContext) { return(dbContext.People.Count()); }
public static string GetPersonName(int personID, AdventureWorks2016CTP3Entities dbContext) { Person person = dbContext.People.FirstOrDefault(p => p.PersonID == personID); return(person.Name); }