本文整理汇总了C#中NorthwindEntities.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# NorthwindEntities.SaveChanges方法的具体用法?C# NorthwindEntities.SaveChanges怎么用?C# NorthwindEntities.SaveChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NorthwindEntities
的用法示例。
在下文中一共展示了NorthwindEntities.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertCustomer
public static void InsertCustomer(string customerId, string companyName, string contactName, string contactTitle,
string adress, string city, string region, string postalCode, string country, string phone, string fax)
{
if (customerId.Length == 0 || customerId.Length>5)
{
throw new ArgumentException("The lenght of CustomerId must be between 0 and 5 characters!");
}
using (NorthwindEntities nwDb = new NorthwindEntities())
{
Customer cusomer = new Customer
{
CustomerID = customerId,
CompanyName = companyName,
ContactName = contactName,
ContactTitle = contactTitle,
Address = adress,
City = city,
Region = region,
PostalCode = postalCode,
Country = country,
Phone = phone,
Fax = fax
};
nwDb.Customers.Add(cusomer);
nwDb.SaveChanges();
}
}
示例2: DeleteCustomer
public static void DeleteCustomer(NorthwindEntities dbNorthwnd)
{
var customer = dbNorthwnd.Customers.Where(z => z.CustomerID == "AAAA1").First();
dbNorthwnd.Customers.Remove(customer);
var affectedRows = dbNorthwnd.SaveChanges();
Console.WriteLine("({0} row(s) affected)", affectedRows);
}
示例3: InsertCustomer
public static void InsertCustomer(string customerID, string companyName, string contactName = null, string contactTitle = null,
string address = null, string city = null, string region = null, string postalCode = null,
string country = null, string phone = null, string fax = null)
{
using (NorthwindEntities northwindDBContext = new NorthwindEntities())
{
Customer customer = new Customer
{
CustomerID = customerID,
CompanyName = companyName,
ContactName = contactName,
ContactTitle = contactTitle,
Address = address,
City = city,
Region = region,
PostalCode = postalCode,
Country = country,
Phone = phone,
Fax = fax
};
northwindDBContext.Customers.Add(customer);
northwindDBContext.SaveChanges();
Console.WriteLine("Row is inserted.");
}
}
示例4: Main
// First way
static void Main()
{
using (NorthwindEntities firstDB = new NorthwindEntities())
{
using (NorthwindEntities secondDB = new NorthwindEntities())
{
var firstCustomer =
(from c in firstDB.Customers
where c.CustomerID == "PARIS"
select c).First();
var secondCustomer =
(from c in secondDB.Customers
where c.CustomerID == "PARIS"
select c).First();
firstCustomer.CompanyName = "First Change with LINQ";
secondCustomer.ContactName = "Second Change with LINQ";
firstDB.SaveChanges();
secondDB.SaveChanges();
Console.WriteLine("Changes are made successfully!");
//SecondWayForChangeRecords();
}
}
}
示例5: ModifyProductName
static void ModifyProductName(int productId, string newName)
{
NorthwindEntities northwindEntities = new NorthwindEntities();
Product product = GetProductById(northwindEntities, productId);
product.ProductName = newName;
northwindEntities.SaveChanges();
}
示例6: Main
static void Main(string[] args)
{
Customer newCustmer = new Customer();
newCustmer.CustomerID = "KULO";
newCustmer.CompanyName = "Mala";
newCustmer.ContactName = "Misoto Kulano";
newCustmer.ContactTitle = "Owner";
newCustmer.Address = "Amela str 23";
newCustmer.City = "Pelon";
newCustmer.PostalCode = "1231";
newCustmer.Country = "France";
newCustmer.Phone = "3443-4323-432";
newCustmer.Fax = "3245-243";
using (var otherDataBase = new NorthwindEntities())
{
using (var dataBase = new NorthwindEntities())
{
otherDataBase.Customers.Add(newCustmer);
otherDataBase.SaveChanges();
//Customer customer = dataBase.Customers.First(x => x.CustomerID == "KULO");
dataBase.Customers.Attach(newCustmer);
dataBase.Customers.Remove(newCustmer);
dataBase.SaveChanges();
}
}
}
示例7: InsertOrder
static void InsertOrder(
string shipName, string shipAddress,
string shipCity, string shipRegionm,
string shipPostalCode,string shipCountry,
string customerID = null, int? employeeID = null,
DateTime? orderDate = null, DateTime? requiredDate = null,
DateTime? shippedDate = null, int? shipVia = null,
decimal? freight = null)
{
using (NorthwindEntities context = new NorthwindEntities())
{
Order newOrder = new Order
{
ShipAddress = shipAddress,
ShipCity = shipCity,
ShipCountry = shipCountry,
ShipName = shipName,
ShippedDate = shippedDate,
ShipPostalCode = shipPostalCode,
ShipRegion = shipRegionm,
ShipVia = shipVia,
EmployeeID = employeeID,
OrderDate = orderDate,
RequiredDate = requiredDate,
Freight = freight,
CustomerID = customerID
};
context.Orders.Add(newOrder);
context.SaveChanges();
Console.WriteLine("Row is inserted.");
}
}
示例8: DeleteProduct
static void DeleteProduct(int productId)
{
NorthwindEntities northwindEntities = new NorthwindEntities();
Product product = GetProductById(northwindEntities, productId);
northwindEntities.Products.Remove(product);
northwindEntities.SaveChanges();
}
示例9: PlaceNewOrder
static void PlaceNewOrder(string customerId,int employeeId,Order_Detail[] details)
{
using (NorthwindEntities nwDb = new NorthwindEntities())
{
using (TransactionScope scope = new TransactionScope())
{
Order order = new Order
{
CustomerID = customerId,
EmployeeID = employeeId,
ShipVia = 1,
ShipName = "UnknownShip",
ShipAddress = "UnknownAddress",
ShipCity = "Unknown",
ShipRegion = "Unknown",
ShipPostalCode = "121311",
ShipCountry = "Unknown",
Order_Details = details
};
nwDb.Orders.Add(order);
nwDb.SaveChanges();
scope.Complete();
}
}
}
示例10: AddCustomer
public static void AddCustomer(NorthwindEntities dbNorthwnd, string name)
{
var customer = new Customer()
{
CustomerID = "AAAA" + name,
CompanyName = "YYYY" + name,
ContactName = "Pesho Peshev",
ContactTitle = "Shef",
Address = "aaaaaaaaaa",
City = "Sofia",
PostalCode = "1330",
Country = "Bulgaria",
Phone = "0000000",
Fax = "0000000"
};
dbNorthwnd.Customers.Add(customer);
try
{
dbNorthwnd.SaveChanges();
}
catch (Exception ex)
{
}
}
示例11: Main
static void Main(string[] args)
{
NorthwindEntities firstContext = new NorthwindEntities();
NorthwindEntities secondContext = new NorthwindEntities();
firstContext.Customers.Add(new Customer() { CustomerID = "ALABA", CompanyName = "Some company" });
firstContext.SaveChanges();
//the second command will be the final because there is no conflict resolution
firstContext.Customers.Find("ALABA").ContactName = "Bacho Kiro";
secondContext.Customers.Find("ALABA").ContactName = "Bai Stavri";
firstContext.SaveChanges();
secondContext.SaveChanges();
}
示例12: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//NorthwindEntities objectContext = new NorthwindEntities();
////inner join
//var result = from cat in objectContext.Categories
// join prod in objectContext.Products
// on cat.CategoryID equals prod.CategoryID
// group cat by cat.CategoryID into groupedCategories
// select new { CategoryID = groupedCategories.Key };
//GridView1.DataSource = result;
//GridView1.DataBind();
////outer join
//var result1 = from cat in objectContext.Categories
// join prod in objectContext.Products
// on cat.CategoryID equals prod.CategoryID
// into groupedCategories
// select new { CategoryID = cat.CategoryID };
//GridView2.DataSource = result1;
//GridView2.DataBind();
//var result2 = objectContext.Categories.Where((item) => (item.CategoryID == 287389)).Select((item) => (item)).First();
//result2.CategoryName = "mumbai";
//objectContext.Categories.AddObject( new Category() { CategoryID = 876221, CategoryName = "India" });
//objectContext.SaveChanges();
//var result3 = objectContext.Categories.First<Category>((cat) => (cat.CategoryID == 65985));
//result3.CategoryName = "london";
//objectContext.SaveChanges();
//var result4 = objectContext.Categories.First<Category>((cat) => (cat.CategoryID == 876221));
//objectContext.DeleteObject(result4);
//objectContext.SaveChanges();
//var result5 = objectContext.Categories.Where((cat) => (cat.CategoryID == 287389)).First<Category>();
//Product prod1 = new Product() { CategoryID = result5.CategoryID, ProductID = 72561, ProductName = "film", UnitPrice = 56, UnitsInStock = 132, Category = result5 };
//objectContext.Products.AddObject(prod1);
//objectContext.SaveChanges();
NorthwindEntities end1 = new NorthwindEntities();
var result6 = from item in end1.Products
select item;
GridView1.DataSource = result6;
GridView1.DataBind();
var result7 = from cat in end1.Categories
join prod in end1.Products
on cat.CategoryID equals prod.CategoryID
select new { CategoryName = cat.CategoryName, ProductName = prod.ProductName };
GridView1.DataSource = result7;
GridView1.DataBind();
var result8 = end1.Categories.Where<Category>((cat) => (cat.CategoryID == 287389)).First<Category>();
result8.CategoryName = "paris";
end1.SaveChanges();
}
示例13: Update
public static void Update(string id, string companyName)
{
var northwindEntities = new NorthwindEntities();
Customer customer = northwindEntities.Customers.FirstOrDefault(
c => c.CustomerID == id);
customer.CompanyName = companyName;
northwindEntities.SaveChanges();
Console.WriteLine("Customer with id: {0} successfully eddited", id);
}
示例14: Delete
public static void Delete(string id)
{
var northwindEntities = new NorthwindEntities();
Customer customer = northwindEntities.Customers.FirstOrDefault(
c => c.CustomerID == id);
northwindEntities.Customers.Remove(customer);
northwindEntities.SaveChanges();
Console.WriteLine("Customer with ID: {0} successfully removed! ", id);
}
示例15: UpdateProduct
static void UpdateProduct(Product product, string newName)
{
using (NorthwindEntities northwindEntities = new NorthwindEntities())
{
northwindEntities.Products.Attach(product); // This line is required!
product.ProductName = newName;
northwindEntities.SaveChanges();
}
}