Forum Xamarin.Forms

Entity Framework using Xaml pages

PeteSuffolkPeteSuffolk USMember ✭✭

I'm trying to learn how to use Entity Framework in XF using XAML pages rather than just code.

I've been working from the one example I could find here:

As you all probably know I am an antique VB Net developer taking my first steps in Xamarin and c#.

In a multi XAML page application where should I initialise the database and how would this relate to individual pages?


  • AlmaJensen.9398AlmaJensen.9398 USMember ✭✭✭

    Let me start of by saying good luck because you're going to be pioneering some new ground here. There are precedents for what you're trying to do, but any attempt I've tried or other developers I know have met with a lot of frustration. I do believe though that xf development is moving in the direction that you are attempting but I have a feeling that all the bugs may not be worked out here. I'll let you know what I am aware of on how to get this to work, I'd love to hear back about any progress or read any blog posts that you write about it.

    Here's a list of what I've run across when looking into doing this.

    • EF on mobile has to be done using EF7 using .net standard. And I believe will have to be implemented on each individual platform not in the core project. So you'll probably have to use a dependency service.
    • XF itself I've heard of developers getting it running using .net standard. But personally every blog or article I've read about it and tried to follow to get it to work failed miserably. The work arounds to convert a brand new project seemed hacky.
    • Unless VS 2017 has changed this there is no new project template for xf that supports .net standard which would be needed for .net core which is required for EF on mobile.
    • Other developers from the xamarin meetup I attend have suggested that there was an initial big push to support .net standard but there's been some back pedaling on Microsofts part between how 1.x and 2.x of .net standard are going to work especially surrounding the project.json files which had to be custom edited to get this to work that xamarin had pull away from pushing to support .net standard in xf until version 2 had been settled on and stabilized. 1.x introduced too many issues.
    • On a positive note I have heard that EF core has worked on xamarin iOS, or xamarin Android only projects.

    Personally I've given up on trying to get this to work until Xamarin creates a template for XF that is configured as a .net standard project type. For now though I'm going to start playing with VS 2017 on my test laptop and see what how that does. The release of VS 2017 has also renewed my hope that this is going to be much easier to get working in the somewhat near future.

  • PeteSuffolkPeteSuffolk USMember ✭✭


    My project on VS2015 has been converted to .Net standard. I tried doing the same on VS2017 on my laptop with a clean install of Win 10 and the new release of VS2017 in the hope VS2017 would be better; however, I lost the will to live and reverted back to my desktop and VS2015.

    I have entity framework core installed in my test project but I am getting errors on trying to do:

                using (var db = new DatabaseContext(dbPath))
                    // Ensure database is created

    The example in the link in my initial question works but I want to use XAML pages.

    As for ios, I am only interested in Android as my target audience will only use Android devices.

  • AlmaJensen.9398AlmaJensen.9398 USMember ✭✭✭

    If you're targeting only xamarin android and not using Xamarin Forms I seem to recall another Xamarin developer I know did get EF Core to work. I'll ping him and see if he can shed any light on this. But for now I would have no real hope of getting EF working as part of a Xamarin Forms project.

    If you got a Xamarin Forms project to actual build and run after being converted to .net standard then you got further than me. I could convert the projects easy enough, but getting them to actually build without hair pulling errors was another matter.

    As far as I'm aware of you can't use Xaml without XF and XF works miserably if at all with .net standard. At least no body I personally know has gotten that to work.

  • PeteSuffolkPeteSuffolk USMember ✭✭


    I followed this to convert to .net standard with vs2015.

    I did:

    create a new xaml forms project.
    remove xamarin.Forms
    Convert to .net Standard 1.60
    edit projects.json to
    "supports": {},
    "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.1.1",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "Xamarin.Forms": ""
    "frameworks": {
    "netstandard1.3": {
    "imports": "portable-net45+win8+wpa81"
    added Xamarin.Forms again.
    and that was it.

    Oh I have a tendency to save the project, close it and re-open it in VS2015 as I got fed up when I started getting several hundred errors if I didn't.

    I tried the same with vs2017 and gave up.

  • PeteSuffolkPeteSuffolk USMember ✭✭

    With a lot of help from Adam Pedley I have the first bit working. The trick was to include a folder path.

    My working but messy DBContext is now:

    public class DatabaseContext : DbContext
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Product> Products { get; set; }
        private string _databasePath = "test.db";
        public DatabaseContext(string databasePath)
            _databasePath = databasePath;
        protected override  void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            IFolder rootFolder = FileSystem.Current.LocalStorage;
            var dbpath = rootFolder.Path.ToString();
            dbpath = Path.Combine(dbpath, "test.db");
            var connectionStringBuilder = new SqliteConnectionStringBuilder
                DataSource = dbpath
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

    I used pclStorage to get a folder but I was just being lazy.

    Each page will need to open the database unless someone can think of another way.


            db = new DatabaseContext(dbPath); // dbPath is ignored in the above code but I will change it later.
Sign In or Register to comment.