Created
November 12, 2018 13:26
-
-
Save danielplawgo/a97aea0dd6b54f4343b4c360764f2eb9 to your computer and use it in GitHub Desktop.
EF Plus - aktualizacja wielu obiektów jednym zapytaniem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseModel | |
{ | |
public BaseModel() | |
{ | |
IsActive = true; | |
} | |
public int Id { get; set; } | |
public bool IsActive { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Category : BaseModel | |
{ | |
public string Name { get; set; } | |
public virtual ICollection<Product> Products { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void DeleteWithEFPlus() | |
{ | |
using (var db = new DataContext()) | |
{ | |
using (var transaction = db.Database.BeginTransaction()) | |
{ | |
var category = db.Categories.FirstOrDefault(c => c.IsActive); | |
if (category == null) | |
{ | |
return; | |
} | |
category.IsActive = false; | |
db.Products.Where(p => p.CategoryId == category.Id) | |
.Delete(); | |
db.SaveChanges(); | |
transaction.Commit(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product : BaseModel | |
{ | |
public string Name { get; set; } | |
public int CategoryId { get; set; } | |
public virtual Category Category { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void SoftDelete() | |
{ | |
using (var db = new DataContext()) | |
{ | |
var category = db.Categories.FirstOrDefault(c => c.IsActive); | |
if (category == null) | |
{ | |
return; | |
} | |
category.IsActive = false; | |
foreach (var product in category.Products) | |
{ | |
product.IsActive = false; | |
} | |
db.SaveChanges(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void SoftDeleteWithEFPlus() | |
{ | |
using (var db = new DataContext()) | |
{ | |
var category = db.Categories.FirstOrDefault(c => c.IsActive); | |
if (category == null) | |
{ | |
return; | |
} | |
category.IsActive = false; | |
db.Products.Where(p => p.CategoryId == category.Id) | |
.Update(p => new Product() { IsActive = false }); | |
db.SaveChanges(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void SoftDeleteWithEFPlusWithTransaction() | |
{ | |
using (var db = new DataContext()) | |
{ | |
using (var transaction = db.Database.BeginTransaction()) | |
{ | |
var category = db.Categories.FirstOrDefault(c => c.IsActive); | |
if (category == null) | |
{ | |
return; | |
} | |
category.IsActive = false; | |
db.Products.Where(p => p.CategoryId == category.Id) | |
.Update(p => new Product() { IsActive = false }); | |
db.SaveChanges(); | |
transaction.Commit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment