2017-02-09

Data is a big part of any application development and mobile apps are no exception; the way we handle data as developers is one of the many important decisions we must make for our mobile apps. From key-value stores to SQLite, there are many options available, but one that .NET developers are often especially familiar with is the Entity Framework.

Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects and eliminates the need for more of the data-access code that developers usually need to write. Entity Framework is great, but was difficult to use in mobile development projects—until Entity Framework Core came along. Entity Framework Core is a lightweight, extensible, cross-platform version of Entity Framework data access technology.

What is Entity Framework Core?

Entity Framework Core (EF Core) introduces many new features and improvements when compared to full-scale Entity Framework, but is a completely brand new codebase that’s optimized for cross-platform applications. If you have experience with Entity Framework, you’ll find EF Core very familiar. Even though it doesn’t have all of the same features, many will show up in future releases (such as lazy loading and connection resiliency). Because EF Core is .NET Standard-compatible, we can now use it with Xamarin.Android.

Due to it’s ease-of-use, EF Core has been one of my favorite projects for quite some time. The challenge has always been, “how can I use the Entity Framework in my Xamarin apps?” In this blog post, I’ll show you how to do just that by building an Android app with a backing Entity Framework Core database.

Getting Started

Creating the .NET Standard Library

To get started, let’s create a .NET Standard library. .NET Standard is a formal specification of the .NET APIs that are intended to be available on all .NET runtimes, similar to Portable Class Libraries. This library is where we’ll place all of our shared application code, including our Entity Framework Core logic.

Create a new solution and add a Portable Class Library (PCL) to that solution.



Since this isn’t a .NET Standard (netstandard) library, we can convert it by right-clicking the project and navigating to the Properties:



According to NuGet, the requirements of the Microsoft.EntityFrameworkCore and Microsoft.EntityFramworkCore.Sqlite packages specify that the library needs to target netstandard 1.3. We can change the target in the same place:



We then need to add the Entity Framework packages to our project. Right-click the References folder, select Manage NuGet Packages…, and install the Microsoft.EntityFrameworkCore and Microsoft.EntityFramworkCore.Sqlite packages.

Great! Now we’re ready to start diving into some Entity Framework code.

Defining the DbContext

If you’ve used Entity Framework before, you’ll be very familiar with how we define a DbContext and our underlying Models that define our database schema.

Let’s start with a simple data model that we’ll call Cat.

Next, let’s ensure that our Cat class is part of our DbContext by defining a new context called CatContext.

Using Entity Framework Core with Xamarin.Android

Getting Started

Now that we have our database configured in our .NET Standard library with EF Core, let’s create an Android project to connect to this database from. Right-click the solution, click Add Project, and select Single-View App (Android).

Just like before, we need to add the
Microsoft.EntityFrameworkCore and Microsoft.EntityFramworkCore.Sqlite NuGet packages to our Android project.

Implementing the Context

First, we need to make sure our Xamarin.Android project is referencing our .NET Standard library. In the Android project, right-click the References folder, select Add Reference…, and then select the library we created at the beginning of this blog post. Click OK to save this reference.

Now that we have that, let’s implement our MainActivity.cs with some Entity Framework code!

Show more