本文整理汇总了C#中Context.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# Context.SaveChanges方法的具体用法?C# Context.SaveChanges怎么用?C# Context.SaveChanges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Context
的用法示例。
在下文中一共展示了Context.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Remove
public void Remove(Book book)
{
using (var ctx = new Context())
{
if (book.Cover != null)
{
ctx.Covers.Attach(book.Cover);
ctx.Entry(book.Cover).State = EntityState.Deleted;
ctx.SaveChanges();
}
if (book.BookMarks != null && book.BookMarks.Count > 0)
{
foreach (var bookmark in book.BookMarks)
{
ctx.Bookmarks.Attach(bookmark);
ctx.Entry(bookmark).State = EntityState.Deleted;
ctx.SaveChanges();
}
}
ctx.Books.Attach(book);
ctx.Entry(book).State = EntityState.Deleted;
ctx.SaveChanges();
}
}
示例2: FetchPage
private static void FetchPage(int year, int area, int pageNumber)
{
using (var context = new Context())
{
var page = GetContent(string.Format(_urlFormat, area, year, pageNumber));
var rows = page.DocumentNode.SelectNodes("//div[@class='list']/table/tbody/tr");
foreach (var row in rows)
{
var artistName = row.SelectSingleNode("td/h2").InnerText;
var projectName = row.SelectSingleNode("td[1]/p").InnerText;
var type = row.SelectSingleNode("td[2]").ChildNodes.First().InnerText.Trim();
var giver = row.SelectSingleNode("td[2]/p").InnerText.Replace("Tildelt af: ", "");
decimal amount;
decimal.TryParse(row.SelectSingleNode("td[3]").InnerText, NumberStyles.Any, CultureInfo.GetCultureInfo("da"), out amount);
var date = row.SelectSingleNode("td[4]").InnerText;
context.Grants.Add(new Grant
{
Amount = amount,
Area = _areaMap[area],
ArtistName = artistName,
Date = date,
Giver = giver,
ProjectName = projectName,
Type = type,
Year = year,
});
}
context.SaveChanges();
Console.WriteLine("Retrieved {2}, {0} page {1}", year, pageNumber, _areaMap[area]);
}
}
示例3: ShowDoTest
public static void ShowDoTest()
{
using (var db = new Context())
{
var dts = new List<DoTest>
{
new DoTest
{
Name = ".Net",
Result = 25,
Time = 25,
Tests = ts,
},
new DoTest
{
Name = "JS",
Result = 12,
Time = 50,
Tests = ts,
},
new DoTest
{
Name = "PHP",
Result = 22,
Time = 15,
Tests = ts,
}
};
db.SaveChanges();
var some = db.testdo.Select(item => item).ToList();
}
}
示例4: InsertOrderInTime
public static Entities.OrderInTime InsertOrderInTime(Context context, Entities.OrderInTime orderIT)
{
Entities.OrderInTime order = new Entities.OrderInTime { TableID = orderIT.TableID, WaiterID = orderIT.WaiterID };
context.OrdersInTime.Add(order);
context.SaveChanges();
return order;
}
示例5: using
//TPH继承
//public static void TPH继承()
//{
// using (var context = new Context())
// {
// var employee = new Employee
// {
// Name = "TPH继承 Employee类的对象",
// Email = "[email protected]",
// PhoneNumber = "12345678",
// Salary = 1234
// };
// var vendor = new Vendor
// {
// Name = "TPH继承 Vendor类的对象",
// Email = "[email protected]",
// PhoneNumber = "78956131",
// HourlyRate = DateTime.MaxValue
// };
// context.TPHPerson.Add(employee);
// context.TPHPerson.Add(vendor);
// context.SaveChanges();
// }
//}
//TPC继承
public static void TPC继承()
{
using (var context = new Context())
{
var employee = new Employee
{
Name = "TPC继承 Employee类的对象",
Email = "[email protected]",
PhoneNumber = "12345678",
Salary = 1234
};
var vendor = new Vendor
{
Name = "TPC继承 Vendor类的对象",
Email = "[email protected]",
PhoneNumber = "78956131",
HourlyRate = DateTime.MaxValue
};
context.People.Add(employee);
context.People.Add(vendor);
context.SaveChanges();
}
}
示例6: AlteraSenha
public bool AlteraSenha(ChangePassword changePassword)
{
string senhaAtualCriptografada = Crypt.Encrypt(changePassword.SenhaAtual);
using (Context db = new Context())
{
Usuario usuario = (from u in db.Usuario
where u.Senha.Equals(Crypt.Encrypt(senhaAtualCriptografada))
select u).FirstOrDefault();
if (usuario != null)
{
// Criptografa e altera a nova senha do usuário
usuario.Senha = Crypt.Encrypt(changePassword.NovaSenha);
// Evita erro de validação do modelo
usuario.ConfirmacaoSenha = usuario.Senha;
db.Entry(usuario).State = EntityState.Modified;
db.SaveChanges();
return true;
}
else
{
return false;
}
}
}
示例7: CreateBlog
private static void CreateBlog()
{
var blog = new Blog {BloggerName = "Julie", Title = "EF41 Blog", DateCreated=DateTime.Now};
//var blog = new Blog() { Title = "This is a blog with a really long blog title" };
var db = new Context();
db.Blogs.Add(blog);
try
{
db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var entityError in ex.EntityValidationErrors)
{
Console.WriteLine(entityError.Entry.Entity.GetType().Name);
foreach (var error in entityError.ValidationErrors)
{
Console.WriteLine("{0}: {1}", error.PropertyName, error.ErrorMessage);
}
}
Console.ReadKey();
}
}
示例8: InitStore
public void InitStore()
{
var db = new Context();
var cat1 = new Category() { Name = "Produto" };
var cat2 = new Category() { Name = "Servico" };
db.Category.Add(cat1); db.Category.Add(cat2);
var i1 = new Item() { Name = "Ração Whiskas", Category = cat1 };
var i2 = new Item() { Name = "Banho", Category = cat2 };
var i3 = new Item() { Name = "Tosa", Category = cat2 };
db.Item.Add(i1);
db.Item.Add(i2);
db.Item.Add(i3);
var c1 = new Client() { Name = "Rafael", Email = "[email protected]" };
var c2 = new Client() { Name = "Monica", Email = "[email protected]" };
db.Client.Add(c1);
db.Client.Add(c2);
db.SaveChanges();
}
示例9: Button_Click
private void Button_Click(object sender, RoutedEventArgs e)
{
try// на случай пустой или битой строки
{
if (dataGrid2.SelectedItem != null)
{
var selectedBook = (Book)dataGrid2.SelectedItem;
var tempBook = new BookOrganizer.Book() { Title = selectedBook.Name, Annotation = selectedBook.Annotation, Author = new Author() { Name = selectedBook.Author }, Year = int.Parse(selectedBook.Year), Pages = int.Parse(selectedBook.Pages) };
var v = new AddBookView();
v.DataContext = new AddBookViewModel(tempBook);
((AddBookViewModel)v.DataContext).BookOut += (b) =>
{
using (var c = new Context())
{
if (b.Author != null)
{
var k = c.Authors.FirstOrDefault(p => p.Name == b.Author.Name);
if (k != null) { b.Author = k; }
}
if (b.Genre != null)
{
var t = c.Genres.FirstOrDefault(p => p.Name == b.Genre.Name);
if (t != null) { b.Genre = t; }
}
c.Books.Add(b);
c.SaveChanges();
}
v.Close();
};
v.Show();
}
}
catch { }
}
示例10: Delete
public ActionResult Delete(int id)
{
_6_Create.Models.Context con = new Context();
Departament dep = con.Departamentos.Single(a=>a.ID==id);
con.Departamentos.Remove(dep);
con.SaveChanges();
return RedirectToAction("Index");
}
示例11: AddMessage
public void AddMessage(Message msg)
{
using (var ctx = new Context())
{
ctx.Messages.Add(msg);
ctx.SaveChanges();
}
}
示例12: Insert
public void Insert(Opcional opcional)
{
using(Context db = new Context())
{
db.Opcional.Add(opcional);
db.SaveChanges();
}
}
示例13: Insert
public void Insert(Lanche lanche)
{
using(Context db = new Context())
{
db.Lanche.Add(lanche);
db.SaveChanges();
}
}
示例14: Update
public void Update(Opcional opcional)
{
using(Context db = new Context())
{
db.Entry(opcional).State = EntityState.Modified;
db.SaveChanges();
}
}
示例15: Update
public void Update(Parametro parametro)
{
using (Context db = new Context())
{
db.Entry(parametro).State = EntityState.Modified;
db.SaveChanges();
}
}