public static Author CreateOrLoadAuthor(string author, BookstoreEntities context) { Author currentAuthor = context.Authors.FirstOrDefault(x => x.Name.ToLower() == author.ToLower()); if (currentAuthor == null) { currentAuthor = new Author(); currentAuthor.Name = author; context.Authors.Add(currentAuthor); context.SaveChanges(); } return currentAuthor; }
private Author CreateOrLoadAuthor(BookstoreEntities context, string authorName) { Author author = context.Authors.FirstOrDefault<Author>(a => a.Name == authorName); if (author != null) { return author; } Author newAuthor = new Author() { Name = authorName }; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
public Author CreateOrLoadAuthor(BookstoreEntities context, string authorName) { Author existingAuthor = context.Authors.Where(a => a.Name == authorName) .FirstOrDefault(); if (existingAuthor != null) { return existingAuthor; } Author newAuthor = new Author(); newAuthor.Name = authorName; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
private static Author CreateOrLoadAuthor(BookstoreEntities context, string author) { Author existingAuthor = (from a in context.Authors where a.AuthorName == author select a).FirstOrDefault(); if (existingAuthor != null) { return existingAuthor; } Author newAuthor = new Author(); newAuthor.AuthorName = author; context.Authors.Add(newAuthor); return newAuthor; }
public static Author CreateOrLoadAuthor(BookstoreEntities context, string name) { Author existingAuthor = (from a in context.Authors where a.Name.ToLower() == name.ToLower() select a).FirstOrDefault(); if (existingAuthor != null) { return existingAuthor; } Author newAuthor = new Author(); newAuthor.Name = name; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
private static Author GetSingleAuthor(BookstoreEntities context, XmlNode authorItem) { if (authorItem.Name != "author" || authorItem.InnerText.Length == 0) { throw new ApplicationException("Invalid input XML file - Invalid Author tag"); } string authorName = authorItem.InnerText; var author = context.Authors.Where(a => a.Name == authorName).ToList(); if (author.Count == 0) { var newAuthor = new Author(); newAuthor.Name = authorName; context.Authors.Add(newAuthor); context.SaveChanges(); author.Add(newAuthor); } return author[0]; }
//private static int GetAuthorId(BookstoreDBEntities context, string name) //{ // var authorid = context.Authors.Where(x => x.Name == name).Select(x => x.AuthorId).First(); // return authorid; //} private static Author CreateOrLoadAuthor( BookstoreDBEntities context, string author) { Author existingUser = (from u in context.Authors where u.Name.ToLower() == author.ToLower() select u).FirstOrDefault(); if (existingUser != null) { return existingUser; } Author newUser = new Author(); newUser.Name = author; context.Authors.Add(newUser); context.SaveChanges(); return newUser; }