document.write("
public List<CardViewModel> Search(string searchWord)
{
try
{
List<CardViewModel> result = new List<CardViewModel>();
using (var context = new CardHolderContext())
{
result = (from crd in context.Cards
join cat in context.CategoryMasters on crd.CategoryId equals cat.CategoryId
join con in context.ContactDetails on crd.CardId equals con.CardId
where
crd.CompanyName.Contains(searchWord) ||
crd.OwnerName.Contains(searchWord) ||
cat.MasterValue.Contains(searchWord)
select new CardViewModel()
{
CardDetails = crd,
Category = cat,
Contact = con
}
).ToList();
}
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Search - Snippet hosted by \"Cacher\"
public void Update(CardViewModel card)
{
try
{
using (var context = new CardHolderContext())
{
var updateCard = context.Cards.Where(x => x.CardId == card.CardDetails.CardId).FirstOrDefault();
updateCard.CompanyName = card.CardDetails.CompanyName;
updateCard.CategoryId = card.CardDetails.CategoryId;
updateCard.JobTitle = card.CardDetails.JobTitle;
updateCard.OwnerName = card.CardDetails.OwnerName;
updateCard.Tagline = card.CardDetails.JobTitle;
updateCard.Website = card.CardDetails.Website;
context.SaveChanges();
var updateContact = context.ContactDetails.Where(x => x.CardId == card.CardDetails.CardId).FirstOrDefault();
updateContact.City = card.Contact.City ;
updateContact.Country = card.Contact.Country;
updateContact.Email = card.Contact.Email;
updateContact.FixNumber = card.Contact.FixNumber;
updateContact.MobileNumber = card.Contact.MobileNumber;
updateContact.Street = card.Contact.Street;
context.SaveChanges();
var deleteSocial = context.SocialMedia.Where(x => x.CardId == card.CardDetails.CardId).ToList();
context.SocialMedia.RemoveRange(deleteSocial);
context.SaveChanges();
if (card.SocialMedia.Count > 0)
{
foreach (var item in card.SocialMedia)
{
item.CardId = card.CardDetails.CardId;
item.SocialId = 0;
context.SocialMedia.Add(item);
}
}
context.SaveChanges();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Update - Snippet hosted by \"Cacher\"
public void Delete(int id)
{
try
{
using (var context = new CardHolderContext())
{
var deleteCard = context.Cards.Where(x => x.CardId == id).FirstOrDefault();
context.Cards.Remove(deleteCard);
var deleteSocial = context.SocialMedia.Where(x => x.CardId == id).ToList();
context.SocialMedia.RemoveRange(deleteSocial);
context.SaveChanges();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Delete - Snippet hosted by \"Cacher\"
public CardViewModel Read(int id)
{
try
{
CardViewModel result = new CardViewModel();
using (var context = new CardHolderContext())
{
result = (from crd in context.Cards
where crd.CardId == id
join cat in context.CategoryMasters on crd.CategoryId equals cat.CategoryId
join con in context.ContactDetails on crd.CardId equals con.CardId
select new CardViewModel()
{
CardDetails = crd,
Category = cat,
Contact = con
}
).FirstOrDefault();
if (result == null)
throw new Exception("Invalid id");
result.SocialMedia = new List<SocialMedium>();
result.SocialMedia = (from soc in context.SocialMedia
where soc.CardId == id
select soc
).ToList();
}
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Read - Snippet hosted by \"Cacher\"
public void Create(CardViewModel card)
{
try
{
using (var context = new CardHolderContext())
{
context.Cards.Add(card.CardDetails);
context.SaveChanges();
card.Contact.CardId = card.CardDetails.CardId;
context.ContactDetails.Add(card.Contact);
if (card.SocialMedia.Count > 0)
{
foreach (var item in card.SocialMedia)
{
item.CardId = card.CardDetails.CardId;
}
}
context.SocialMedia.AddRange(card.SocialMedia);
context.SaveChanges();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
CreateCards - Snippet hosted by \"Cacher\"
public List<CardViewModel> Retrieve()
{
try
{
List<CardViewModel> result = new List<CardViewModel>();
using (var context = new CardHolderContext())
{
result = (from crd in context.Cards
join cat in context.CategoryMasters on crd.CategoryId equals cat.CategoryId
join con in context.ContactDetails on crd.CardId equals con.CardId
select new CardViewModel()
{
CardDetails = crd,
Category = cat,
Contact = con
}
).ToList();
}
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
RetrieveCards - Snippet hosted by \"Cacher\"
");