Skip to content

Commit

Permalink
Finished EF Core Demo
Browse files Browse the repository at this point in the history
Currently this works for Android only and not for iOS.

See dotnet/efcore#7158 for details
  • Loading branch information
AndreKraemer committed May 5, 2017
1 parent 9c50590 commit f99e22e
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Button Clicked="EditApplicationBaseDataButton_OnClicked" Text="Edit Application Base Data"></Button>
<Button Clicked="SecureDataButton_OnClicked" Text="Secure Data"></Button>
<Button Clicked="SqliteButton_OnClicked" Text="SQLite"></Button>
<Button Clicked="EfCoreButton_OnClicked" Text="Ef Core"></Button>
</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ private void SqliteButton_OnClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new SqliteDemoPage());
}

private void EfCoreButton_OnClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new EfCoreDemoPage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace XamarinLocalDataAccessDemo.Models
{
public class DishDbContext : DbContext
{
private readonly string _databasePath;

public DishDbContext(string databasePath)
{
_databasePath = Path.Combine(databasePath, "dishes.db");
}
public DbSet<Dish> Dishes { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={_databasePath}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinLocalDataAccessDemo.Pages.EfCoreDemoPage"
Title="ListView">
<ListView ItemsSource="{Binding Items}"
IsPullToRefreshEnabled="true"
CachingStrategy="RecycleElement"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RefreshCommand="{Binding RefreshDataCommand}">
<ListView.Header>
<StackLayout Padding="10"
Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
BackgroundColor="#dadada">
<Button Text="Add Entry" Command="{Binding AddCommand}"></Button>
</StackLayout>
</ListView.Header>
<!--Built in Cells-->
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}"
Detail="{Binding Description}"/>
</DataTemplate>
</ListView.ItemTemplate>


</ListView>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XamarinLocalDataAccessDemo.Models;
using XamarinLocalDataAccessDemo.Services;

namespace XamarinLocalDataAccessDemo.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EfCoreDemoPage : ContentPage
{
public EfCoreDemoPage()
{
InitializeComponent ();
BindingContext = new EfCoreDemoPageViewModel();
}

}

class EfCoreDemoPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<Dish> Items { get; }
bool _busy;
private DishDbContext _db;

public EfCoreDemoPageViewModel()
{

var path = DependencyService.Get<IPathService>().GetDatabasePath();
_db = new DishDbContext(path);
_db.Database.EnsureCreated();
RefreshDataCommand = new Command(
async () => await RefreshData());
AddCommand = new Command(
async () => await AddData());

Items = new ObservableCollection<Dish>(_db.Dishes.ToList());
}

public ICommand RefreshDataCommand { get; }
public ICommand AddCommand { get; }

async Task RefreshData()
{
IsBusy = true;
IsBusy = true;

Items.Clear();

foreach (var dish in _db.Dishes)
{
Items.Add(dish);
}
IsBusy = false;
await Task.Delay(2000);

IsBusy = false;
}

async Task AddData()
{
IsBusy = true;
var dish = new Dish
{
Name = $"Ensalada Fantasia ({DateTime.Now:T})",
Description = "Salat aus allen Zutaten, die noch übrig waren",
Price = 1.99,
CategoryId = 1
};
await _db.Dishes.AddAsync(dish);
await _db.SaveChangesAsync();
IsBusy = false;
}

public bool IsBusy
{
get { return _busy; }
set
{
_busy = value;
OnPropertyChanged();
((Command)RefreshDataCommand).ChangeCanExecute();
}
}


public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="sqlite-net-pcl" Version="1.3.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.3.0" />
Expand Down

0 comments on commit f99e22e

Please sign in to comment.