コード例 #1
0
 public void BuildDatabase()
 {
     using (AddrbookDbContext ctx = new AddrbookDbContext(DbPath))
     {
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
     }
 }
コード例 #2
0
 public IEnumerable <Addrbook> LoadAddressBooks()
 {
     using (AddrbookDbContext ctx = new AddrbookDbContext(DbPath))
     {
         foreach (Addrbook item in ctx.Addrbooks.Select(p => p))
         {
             yield return(item);
         }
     }
 }
コード例 #3
0
 public void SaveStoredData(string contact_name, string address)
 {
     using (AddrbookDbContext ctx = new AddrbookDbContext(DbPath))
     {
         SaveStoredData(ctx, contact_name, address);
         try
         {
             ctx.SaveChanges();
         }
         catch (Exception ex)
         {
             string str = ex.Message;
         }
     }
 }
コード例 #4
0
        private static void SaveStoredData(AddrbookDbContext ctx, string contact_name, string address)
        {
            Addrbook addrBook = ctx.Addrbooks.FirstOrDefault(p => p.Address == address);

            if (addrBook == null)
            {
                ctx.Addrbooks.Add(new Addrbook
                {
                    ContactName = contact_name,
                    Address     = address
                });
            }
            else
            {
                addrBook.ContactName = contact_name;
            }
        }
コード例 #5
0
 public bool DeleteAddress(string address)
 {
     using (AddrbookDbContext ctx = new AddrbookDbContext(DbPath))
     {
         Addrbook addrbook = ctx.Addrbooks.FirstOrDefault(p => p.Address.SequenceEqual(address));
         if (address != null)
         {
             ctx.Addrbooks.Remove(addrbook);
             try
             {
                 ctx.SaveChanges();
             }
             catch (Exception ex)
             {
                 string str = ex.Message;
             }
         }
     }
     return(true);
 }