Inheritance: DbContext
Esempio n. 1
0
 public Item GetById( int id )
 {
     using ( TodoContext context = new TodoContext( ) )
     {
         return ( from i in context.Items
                  where i.Id == id
                  select i ).FirstOrDefault( );
     }
 }
Esempio n. 2
0
        public void Add( Item item )
        {
            using ( TodoContext context = new TodoContext( ) )
            {
                item.Id = 1 + this.AllItems.Max( x => ( int? )x.Id ) ?? 0;
                context.Items.Add( item );

                context.SaveChanges( );
            }
        }
Esempio n. 3
0
 public void Init()
 {
     using ( TodoContext context = new TodoContext( ) )
     {
         context.Database.ExecuteSqlCommand("CREATE TABLE Items (Id integer, Title varchar(150), IsDone integer);");
         
         context.Items.Add(new Item { Id = 1, Title = "Task 1", IsDone = true });
         context.Items.Add(new Item { Id = 2, Title = "Task 2", IsDone = false });
         context.Items.Add(new Item { Id = 3, Title = "Task 3", IsDone = true });
         
         context.SaveChanges();
     }
 }
Esempio n. 4
0
        public bool Delete( int id )
        {
            using ( TodoContext context = new TodoContext( ) )
            {
                Item item = ( from i in context.Items
                              where i.Id == id
                              select i ).FirstOrDefault( );
                if ( item == null )
                {
                    return false;
                }
                else
                {
                    context.Items.Remove( item );

                    context.SaveChanges( );

                    return true;
                }
            }
        }