WORKS! == Xamarin.Forms + .NET Standard + Entity Framework Core for SQLite on iOS, Android, UWP!

2»

Posts

  • geedsengeedsen ✭✭ USMember ✭✭
    edited March 2018

    @Christian

    Yes, I can confirm that 15.7 preview solves the problem, don't forget to also install the update on the Mac.
    But... it gives problems, building of the app fails most of the time with random errors, and when it build and starts on the iPhone it now gives a 'The App has been terminated'. ...(It actually started once, I saw the problem was gone, but can not start the App again)

    Ben

  • davefxydavefxy ✭✭ Member ✭✭

    Could you describe the steps you performed in Visual Studio to create the Todo project? I apologize for the hand holding required.

  • ChrisWReaChrisWRea ✭✭ CAMember ✭✭

    @davefxy said:
    Could you describe the steps you performed in Visual Studio to create the Todo project? I apologize for the hand holding required.

    Hi @davefxy. I didn't create the project. The sample is adapted from this Xamarin sample described in the docs. I merely ported it to use EF Core instead of sqlite-net.

    If you want to start a similar project today, I suggest you use the built-in project templates. You can do so via File > New > Project in the menu, and select Visual C# > Cross-Platform in the left hand side of the New Project dialog, and then choose Mobile App (Xamarin.Forms) from the list of project templates, and then the Blank App template in the dialog that follows. Be sure to choose .NET Standard as the Code Sharing Strategy.

    Pay attention to some of the current workarounds earlier in this thread. Notably, for the Android project you may want to add a packages.config to your project instead of using the new PackageReference-style references in the *.csproj file, as there is an open issue where using packages.config is a workaround.

  • davefxydavefxy ✭✭ Member ✭✭

    How did you know what packages to put into the packages.config file? If these are not listed in the csproj file?

  • ChrisWReaChrisWRea ✭✭ CAMember ✭✭

    @davefxy said:
    How did you know what packages to put into the packages.config file? If these are not listed in the csproj file?

    Start with a valid but empty packages.config in your Android project. (See first example below.) Then use the "Manage NuGet Packages" menu item (available if you right-click your project's References) to find and install the necessary packages. Start with Xamarin.Forms, and Microsoft.EntityFrameworkCore.Sqlite for EF Core. The package manager, by default, pulls in the package you ask for, and the packages that it in turn depends on. If you open your packages.config after installing packages, you'll see that's where they end up. (See second example below.)

    Here's what a valid but empty packages.config file looks like:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
    </packages>
    

    After you install some packages, it will look something like:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Microsoft.EntityFrameworkCore" version="2.0.2" targetFramework="monoandroid80" />
      <package id="Microsoft.EntityFrameworkCore.Relational" version="2.0.2" targetFramework="monoandroid80" />
      <package id="Microsoft.EntityFrameworkCore.Sqlite" version="2.0.2" targetFramework="monoandroid80" />
      <package id="Microsoft.EntityFrameworkCore.Sqlite.Core" version="2.0.2" targetFramework="monoandroid80" />
      <!-- ...etc... -->
    </packages>
    
  • rouge1nirouge1ni ✭✭✭ GBMember ✭✭✭
    edited May 2018

    How can you say this works I am trying to get data annotations to work in the standard way and it doesnt. I am using these in my dbcontext in my web api and I want to be transpose it from json to an object so doesnt xamrian still need the dataannotation to understand that in the form of.

    My web api returns json so for example I have the following

    [code]

      public class FuelWc : IHttpService
    {
    readonly IJsonConverter jsonConverter;
        public FuelWc()
        {
    
    
        }
    public FuelWc(IJsonConverter jsonConverter)
    {
        this.jsonConverter = jsonConverter;
    }
    
    public async Task<T> Get<T>(string url)
    {
        using (var client = new System.Net.Http.HttpClient())
        {
            var response = await client.GetAsync(url);
            var json = await response.Content.ReadAsStringAsync();
    
            return jsonConverter.Deserialize<T>(json);
        }
    }
    

    Which I call using

        readonly string url = "http://localhost:81/api ";
        readonly IHttpService httpService;
    
        public FuelApiClient(IHttpService httpService)
        {
            this.httpService = httpService;
        }
    
        public Task<List<JobsList>> GetJobs() => httpService.Get<List<JobsList>>($"{url}JobsLists");
    

    [/code]

    However cause xamrain forms doesn't understand data notations how can i keep my models the same for ef and for the Deserialize of the json to the .net object.

    i use the following
    [code]

    public class JobsList
    {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; } // int
    public int? EnginerId { get; set; } // int
    public int? ClientId { get; set; } // int
    public DateTime? StartTime { get; set; } // date
    public DateTime? EndTime { get; set; } // date
    [MaxLength]
    public string Notes { get; set; } // nvarchar(max)
    [MaxLength(150)]
    public string photo { get; set; } // nvarchar(150)
    public int? AssignedTo { get; set; } // int
    public int? CompletedBy { get; set; } // int
    public DateTime? CreatedDate { get; set; } // date
    public DateTime? CompletedDate { get; set; } // date
    public int? Components { get; set; } // int
    public int? JobStatus { get; set; } // int

        [MaxLength]
        public string JobStatusReason { get; set; } // nvarchar(max)
    
    }
    

    [/code]

    But of course if i included a reference to this the xamrin form player will complain it doesnt like data annotations.

  • MarkZhukovskyMarkZhukovsky ✭✭ USMember ✭✭

    @ChrisWRea You're a true pioneer - thanks for putting this together. Question - now that this issue is closed - what needs to be updated? Or do you have plans to update your sample?

  • ChrisWReaChrisWRea ✭✭ CAMember ✭✭

    @MarkZhukovsky You're welcome. I was intending to revisit the sample when Entity Framework Core 2.1 is released. I'll remove any unnecessary workarounds then (i.e. System.Runtime.CompilerServices.Unsafe) that may have been required prior to VS 15.7.x.

  • Raniel_QuiranteRaniel_Quirante ✭✭ Member ✭✭

    Good day sir Chris, How can I do this with an existing sqlite database? Also, where can I locate the local database created with the code used in this solution?

  • ChrisWReaChrisWRea ✭✭ CAMember ✭✭
    edited June 2018

    @christianquirante01 I haven't reverse-engineered a database with EF Core yet but this might be a good starting point: https://wildermuth.com/2017/12/20/Reverse-Engineering-Existing-Databases-in-Entity-Framework-Core-2. I expect one would do the reverse engineering work in a regular .NET Core project, and then consume the final model classes in the Xamarin project through a .NET Standard library.

    As for where you can find the local database in the Todo sample, it will vary based on the platform. Look for the FileHelper.cs file within each platform project and there you'll see where the database path gets constructed. Here is the iOS app's FileHelper.cs: https://github.com/cwrea/XamarinTodo/blob/NETStd20_EFCore203/Todo.iOS/FileHelper.cs. You might modify yours to output the return value to debug output, so you can see the full path. On my Mac, I use a tool called iExplorer to grab copies of the SQLite database from the app's Library/Databases folder.

  • MarkZhukovskyMarkZhukovsky ✭✭ USMember ✭✭

    It has been confirmed that Azure Mobile Apps unfortunately will not support .NET Core, so the offline sync capability (that comes in their starter solution) with .net core backend is off the table.

    Offline-first approach with bidirectional synchronization would really take this sample to the next level. Wondering if you've given this any thought? I was considering playing around with this free library for SQLite syncing to a SQL db on Azure. @ChrisWRea

  • MarkZhukovskyMarkZhukovsky ✭✭ USMember ✭✭

    Also, are you able to get this to work with the tooling for Migrations?

  • JohanMulderJohanMulder ✭✭ USMember ✭✭

    Hi Chris, Is your sample working now on EF Core SQLite 2.1 or are there still issues ?

  • ChrisWReaChrisWRea ✭✭ CAMember ✭✭
    edited August 2018

    @JohanMulder I can't say as I haven't had the time to try yet, assuming you are referring to having tried with the recent release of Visual Studio 15.8. Client work keeping me busy.

  • cdelaunecdelaune Member

    @ChrisWRea, VS 15.8.1 appears to fix the System.Memory compile error for iOS (github.com/xamarin/xamarin-macios/issues/4168) and I am able to deploy to a physical device, but I am still getting the AOT error related to Nullable columns.

    When you circle back to this, I would be interested to know if you are seeing similar behavior, as the Nullable columns issue is marked as "Closed" in GitHub (github.com/aspnet/EntityFrameworkCore/issues/9249) and "RESOLVED FIXED" in the Xamarin Bugzilla (bugzilla.xamarin.com/show_bug.cgi?id=59184) and (bugzilla.xamarin.com/show_bug.cgi?id=58424).

  • PlamenYovchevPlamenYovchev ✭✭ USMember ✭✭

    Guys, what about migrations ? I know about the console app way and it works but it's a bit annoying.

  • DebbsDebbs Member

    That was cool. Thanks.

Sign In or Register to comment.