本文整理汇总了C#中AppContext.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# AppContext.SaveChanges方法的具体用法?C# AppContext.SaveChanges怎么用?C# AppContext.SaveChanges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AppContext
的用法示例。
在下文中一共展示了AppContext.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PersonAddresses
private static void PersonAddresses(int? skip = 0, int? takecount = 0)
{
var startTime = DateTime.Now;
var w = FluentConsole.Instance;
var db = new ACDBContext();
var count = 0;
var savedCount = 0;
if (takecount == 0) takecount = db.AddressPersonRels.Count();
var entityName = "Person-Address";
w.White.Line($"Creating {takecount} {entityName}s");
using (var context = new AppContext())
{
foreach (var item in db.AddressPersonRels.OrderBy(x => x.Id).Skip(skip ?? 0).Take(takecount ?? 0))
{
count++;
var person = context.Persons.Include("PersonAddresses").FirstOrDefault(x => x.Id == item.CommonPerson.Id);
if (person == null)
{
w.Red.Line($"Error {entityName} {count} of {takecount}: {entityName} not found");
continue;
}
//todo: verify query
if (person.PersonAddresses.Any(
x => x.Street == item.Address.Address1?.Trim() &&
x.City == item.Address.City?.Trim() &&
x.StateId == item.Address.StateId &&
x.PrimaryStatus == (PrimaryStatus)item.PrimaryStatusId))
{
w.Yellow.Line($"Warning {entityName} {count} of {takecount}: {entityName} {person.ReverseFullName}-{item.Address.Address1} already exists");
continue;
}
person.PersonAddresses.Add(new PersonAddress()
{
Street = item.Address.Address1?.Trim(),
Street2 = item.Address.Address2?.Trim(),
City = item.Address.City?.Trim(),
County = item.Address.County?.Trim(),
StateId = item.Address.StateId,
ZipCode = $"{item.Address.Zip5?.Trim()}-{item.Address.Zip4?.Trim()}",
Country = item.Address.Country?.Trim(),
Latitude = item.Address.Latitude,
Longitude = item.Address.Longitude,
PrimaryStatus = (PrimaryStatus)item.PrimaryStatusId,
DateCreated = item.DateCreated,
DateUpdated = item.DateModified
});
person.LogEntries.Add(new PersonLogEntry() { Note = $"Added Address {item.Address.Address1}" });
w.Green.Line($"Adding {count} of {takecount} {entityName}: {person.ReverseFullName}-{item.Address.Address1}");
savedCount++;
context.SaveChanges();
}
w.Gray.Line($"Saving {entityName}s...");
context.SaveChanges();
var totalTime = DateTime.Now - startTime;
w.Green.Line($"Saved {savedCount} {entityName}s in {totalTime.Hours}:{totalTime.Minutes}:{totalTime.Seconds} ");
}
}
示例2: OrganizationActivities
private static void OrganizationActivities(int? skip = 0, int? takecount = 0)
{
var startTime = DateTime.Now;
var w = FluentConsole.Instance;
var db = new ACDBContext();
var count = 0;
var savedCount = 0;
if (takecount == 0) takecount = db.OrganizationStatusHistories.Count();
var entityName = "Organziation-Activity";
w.White.Line($"Creating {takecount} {entityName}s");
using (var context = new AppContext())
{
foreach (var item in db.OrganizationStatusHistories.OrderBy(x => x.Id).Skip(skip ?? 0).Take(takecount ?? 0))
{
count++;
var org = context.Organizations.Include(x => x.OrganizationActivity).FirstOrDefault(x => x.Id == item.OrganizationId);
if (org == null)
{
w.Red.Line($"Error {entityName} {count} of {takecount}: {entityName} not found");
continue;
}
if (org.OrganizationActivity.Any(x => x.ActiveYear == item.ActiveYear))
{
w.Yellow.Line($"Warning {entityName} {count} of {takecount}: {entityName} {org.Name}-{item.ActiveYear} already exists");
continue;
}
org.OrganizationActivity.Add(new OrganizationActivity()
{
ActiveYear = item.ActiveYear,
});
org.LogEntries.Add(new OrganizationLogEntry() { Note = $"Added Activity Year {item.ActiveYear}" });
w.Green.Line($"Adding {count} of {takecount} {entityName}: {org.Name}-{item.ActiveYear}");
savedCount++;
context.SaveChanges();
}
w.Gray.Line($"Saving {entityName}s...");
context.SaveChanges();
var totalTime = DateTime.Now - startTime;
w.Green.Line($"Saved {savedCount} {entityName}s in {totalTime.Hours}:{totalTime.Minutes}:{totalTime.Seconds} ");
w.White.Line(new String('-', 15));
}
}
示例3: Create
public void Create(string username, string password)
{
using (var ctx = new AppContext())
{
ctx.Users.Add(new User { Id = 123123123, Username = username, Password = password });
ctx.SaveChanges();
}
}
示例4: PublicationCorrespondences
private static void PublicationCorrespondences(int? skip = 0, int? takecount = 0)
{
var startTime = DateTime.Now;
var w = FluentConsole.Instance;
var db = new ACDBContext();
var count = 0;
var savedCount = 0;
if (takecount == 0) takecount = db.MediaPublishedMediaCorrespondenceRels.Count();
var entityName = "Publication-Correspondence";
w.White.Line($"Creating {takecount} {entityName}s");
using (var context = new AppContext())
{
foreach (var item in db.MediaPublishedMediaCorrespondenceRels.OrderBy(x => x.Id).Skip(skip ?? 0).Take(takecount ?? 0))
{
count++;
var e = context.Correspondences.Find(item.MediaCorrespondenceId);
var pub =
context.Publications.Include(x => x.Correspondence).FirstOrDefault(x => x.Id == item.MediaPublishedId);
if (e == null || pub == null)
{
w.Red.Line($"Error {entityName} {count} of {takecount}: {entityName} not found");
continue;
}
//todo: change to correspondences
if (pub.Correspondence.Any(x => x.Id == e.Id))
{
w.Yellow.Line($"Warning {entityName} {count} of {takecount}: {entityName} {pub.Name}-{e.Name} already exists");
continue;
}
pub.Correspondence.Add(e);
pub.LogEntries.Add(new PublicationLogEntry() { Note = $"Added Correspondence {e.Name}" });
e.LogEntries.Add(new CorrespondenceLogEntry() { Note = $"Added Publication {pub.Name}" });
w.Green.Line($"Adding {count} of {takecount} {entityName}: {pub.Name}-{e.Name}");
savedCount++;
context.SaveChanges();
}
w.Gray.Line($"Saving {entityName}s...");
context.SaveChanges();
var totalTime = DateTime.Now - startTime;
w.Green.Line($"Saved {savedCount} {entityName}s in {totalTime.Hours}:{totalTime.Minutes}:{totalTime.Seconds} ");
w.White.Line(new String('-', 15));
}
}
示例5: CreateUsers
public void CreateUsers(IEnumerable<User> users)
{
using (var db = new AppContext())
{
foreach (var user in users)
{
db.Users.Add(user);
}
db.SaveChanges();
}
}
示例6: CreateClients
public void CreateClients(IEnumerable<Client> clients)
{
using (var db = new AppContext())
{
foreach (var client in clients)
{
db.Clients.Add(client);
}
db.SaveChanges();
}
}
示例7: CreateEntries
public void CreateEntries(IEnumerable<Entry> entries)
{
using (var db = new AppContext())
{
foreach (var entry in entries)
{
db.Entries.Add(entry);
}
db.SaveChanges();
}
}
示例8: UpdateUser
public void UpdateUser(UserDetails userdetails)
{
if (userdetails.Id == Guid.Empty)
throw new ApplicationException("User Id empty");
using (var db = new AppContext())
{
var user = (from usr in db.Users where usr.Id == userdetails.Id select usr).FirstOrDefault();
var entry = db.Entry<UserDetails>(user);
entry.CurrentValues.SetValues(userdetails);
db.SaveChanges();
}
}
示例9: Main
static void Main(string[] args)
{
using (AppContext ctx = new AppContext())
{
Editora ed = new Editora
{
Nome = "Positivo",
Email = "[email protected]"
};
ctx.Editoras.Add(ed);
ctx.SaveChanges();
}
}
示例10: Update
public void Update(Constituent entity)
{
var isValid = Validate(entity);
if (!isValid) return;
using (var db = new AppContext())
{
var existing = db.Constituents.Find(entity.Id);
if (entity.Equals(existing)) return;
entity.IsUpdated = true;
entity.UpdatedBy = "donor";
db.Constituents.AddOrUpdate(entity);
db.SaveChanges();
}
}
示例11: Main
static void Main(string[] args)
{
using (var db = new AppContext())
{
var pessoa = new Pessoa
{
Nome = "Renan Frutuozo",
TelefoneCelular = "(85) 9 8832-0829",
Email = "[email protected]",
TelefoneFixo = "(85) 3384-6237"
};
db.Pessoas.Add(pessoa);
db.SaveChanges();
}
}
示例12: CreateProjects
public void CreateProjects(IEnumerable<Project> projects)
{
using (var db = new AppContext())
{
foreach (var project in projects)
{
db.Projects.Add(project);
}
try
{
db.SaveChanges();
}
catch (Exception ex)
{
ex.ToString();
}
}
}
示例13: CreateUserIfNew
/// <summary>
/// Creates the user in the database. avoid conflicts we append the name of the provider
/// Ex: Twitter-UserName
/// </summary>
/// <param name="screenName">screen name returned by provider</param>
/// <param name="authprovider"></param>
public UserDetails CreateUserIfNew(string screenName, AuthenticationProvider authprovider)
{
string username = authprovider.ToString() + "-" + screenName;
using (var db = new AppContext())
{
var details = db.Users.Where(u => u.UserName == username).FirstOrDefault();
if (details == null)
{
details = new UserDetails()
{
Id = Guid.NewGuid(),
UserName = username
};
db.Users.Add(details);
db.SaveChanges();
}
return details;
}
}
示例14: ChapterAliases
private static void ChapterAliases(int? skip = 0, int? takecount = 0)
{
var startTime = DateTime.Now;
var w = FluentConsole.Instance;
var db = new ACDBContext();
var count = 0;
var savedCount = 0;
if (takecount == 0) takecount = db.AliasChapterRels.Count();
var entityName = "Chapter-Alias";
w.White.Line($"Creating {takecount} {entityName}s");
using (var context = new AppContext())
{
foreach (var item in db.AliasChapterRels.OrderBy(x => x.Id).Skip(skip ?? 0).Take(takecount ?? 0))
{
count++;
var chapter = context.Chapters.Include(x => x.ChapterAliases).FirstOrDefault(x => x.Id == item.ChapterId);
if (chapter == null)
{
w.Red.Line($"Error {entityName} {count} of {takecount}: {entityName} not found");
continue;
}
//todo: verify query
if (chapter.ChapterAliases.Any(x => x.Name == item.Alias.AliasName && x.PrimaryStatus == (PrimaryStatus)item.PrimaryStatusId))
{
w.Yellow.Line($"Warning {entityName} {count} of {takecount}: {entityName} {chapter.Name}-{item.Alias.AliasName} already exists");
continue;
}
chapter.ChapterAliases.Add(new ChapterAlias()
{
Name = item.Alias.AliasName,
PrimaryStatus = (PrimaryStatus)item.PrimaryStatusId,
DateCreated = item.DateCreated,
DateUpdated = item.DateModified
});
chapter.LogEntries.Add(new ChapterLogEntry() { Note = $"Added Alias {item.Alias.AliasName}" });
w.Green.Line($"Adding {count} of {takecount} {entityName}: {chapter.Name}-{item.Alias.AliasName}");
savedCount++;
context.SaveChanges();
}
w.Gray.Line($"Saving {entityName}s...");
context.SaveChanges();
var totalTime = DateTime.Now - startTime;
w.Green.Line($"Saved {savedCount} {entityName}s in {totalTime.Hours}:{totalTime.Minutes}:{totalTime.Seconds} ");
w.White.Line(new String('-', 15));
}
}
示例15: EventAudioVideos
private static void EventAudioVideos(int? skip = 0, int? takecount = 0)
{
var startTime = DateTime.Now;
var w = FluentConsole.Instance;
var db = new ACDBContext();
var count = 0;
var savedCount = 0;
if (takecount == 0) takecount = db.EventMediaAudioVideoRels.Count();
var entityName = "Event-AudioVideo";
w.White.Line($"Creating {takecount} {entityName}s");
using (var context = new AppContext())
{
foreach (var item in db.EventMediaAudioVideoRels.OrderBy(x => x.Id).Skip(skip ?? 0).Take(takecount ?? 0))
{
count++;
var e = context.AudioVideos.Find(item.MediaAudioVideoId);
var @event = context.Events.Include("AudioVideos").FirstOrDefault(x => x.Id == item.EventId);
if (e == null || @event == null)
{
w.Red.Line($"Error {entityName} {count} of {takecount}: {entityName} not found");
continue;
}
if (@event.AudioVideos.Any(x => x.Id == e.Id))
{
w.Yellow.Line($"Warning {entityName} {count} of {takecount}: {entityName} {@event.Name}-{e.Title} already exists");
continue;
}
@event.AudioVideos.Add(e);
@event.LogEntries.Add(new EventLogEntry() { Note = $"Added Audio Video {e.Title}" });
e.LogEntries.Add(new AudioVideoLogEntry() { Note = $"Added Event {@event.Name}" });
w.Green.Line($"Adding {count} of {takecount} {entityName}: {@event.Name}-{e.Title}");
savedCount++;
context.SaveChanges();
}
w.Gray.Line($"Saving {entityName}s...");
context.SaveChanges();
var totalTime = DateTime.Now - startTime;
w.Green.Line($"Saved {savedCount} {entityName}s in {totalTime.Hours}:{totalTime.Minutes}:{totalTime.Seconds} ");
}
}