0

I am trying to place Pin type objects into the database. The Pin object takes a location (latitude, longitude) and address. Once in the database the objects should be able to be retrieved and displayed on a map.

I am using a MacBook and for some reason the pin.db file won't be created. I have chmod tried. .ensureCreated should take care of that but it does not.

Models/Pin.cs:

using System;
using Microsoft.EntityFrameworkCore;
using mauiMap.Models;

namespace mauiMap.Models
{
    public class Pin
    {
        public int Id { get; set; } //to have some sort of identification
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string Address { get; set; }
        public string Description { get; set; }
    }
}

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
             xmlns:sensors="clr-namespace:Microsoft.Maui.Devices.Sensors;assembly=Microsoft.Maui.Essentials"
             xmlns:maps1="clr-namespace:Microsoft.Maui.Maps;assembly=Microsoft.Maui.Maps"
             x:Class="mauiMap.MainPage">
      <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <maps:Map x:Name="map" Grid.Row="0" ItemsSource="{Binding Pins}">
            <x:Arguments>
                <!--<MapSpan>
                    <x:Arguments>
                        <sensors:Location>
                            <x:Arguments>
                                <x:Double>36.9628066</x:Double>
                                <x:Double>-122.0194722</x:Double>
                            </x:Arguments>
                        </sensors:Location>
                        <x:Double>0.01</x:Double>
                        <x:Double>0.01</x:Double>
                    </x:Arguments>
                </MapSpan>-->
            </x:Arguments>
            <maps:Map.ItemTemplate>
                <DataTemplate>
                    <maps:Pin Location="{Binding Location}"
                              Address="{Binding Address}"
                              Label="{Binding Description}" />
                </DataTemplate>    
            </maps:Map.ItemTemplate>
        </maps:Map>
        <StackLayout Grid.Row="1" Orientation="Vertical">
            <Label Text="Latitude"/>
            <Entry x:Name="_lat" Placeholder="Latitude"/>
            <Label Text="Longitude"/>
            <Entry x:Name="_long" Placeholder="Longitude"/>
            <Label Text="Address"/>
            <Entry x:Name="addr" Placeholder="Address"/>
            <Label Text="Description"/>
            <Entry x:Name="desc" Placeholder="Description"/>
            <Button x:Name="submit" Text="Submit" Clicked="OnSubmit"/> 
        </StackLayout>
    </Grid>

</ContentPage>

MainPage.xaml.cs:

using Microsoft.Maui.Controls;
using Microsoft.Maui.Maps;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using mauiMap.Models;
using System.Net;

namespace mauiMap
{
    public partial class MainPage : ContentPage
    {
        //private vModelMap viewModel;
        private ObservableCollection<PinViewModel> pins;
        private PinDbContext dbContext;

        public MainPage()
        {
            InitializeComponent();
            //viewModel = new vModelMap();
            //this.BindingContext = viewModel;
            pins = new ObservableCollection<PinViewModel>();
            this.BindingContext = pins;

            dbContext = new PinDbContext();
            dbContext.Database.EnsureCreated();

            //Get the pin objects from the database and insert them into the pins collection 
            var pinsDb = dbContext.Pins.ToList();
            foreach(var pin in pinsDb) {
                pins.Add(new PinViewModel
                {
                    Location = new Location(pin.Latitude, pin.Longitude),//Location thinks it's the Android one put Models.Location
                    Address = pin.Address,
                    Description = pin.Description
                });
            }
        }

        public void OnSubmit(object sender, EventArgs e)
        {
            //Entry fields values
            double lat = Convert.ToDouble(_lat.Text);
            double longitude = Convert.ToDouble(_long.Text);
            string address = addr.Text;
            string description = desc.Text;

            //Create a new pin object
            var pin = new Pin
            {
                Latitude = lat,
                Longitude = longitude,
                Address = address,
                Description = description
            };

            //SAVE THE PIN OBJECT TO DB 
            dbContext.Pins.Add(pin);
            dbContext.SaveChanges(); 

            /*
            viewModel.Pins.Add(new PinViewModel
            {
                Location = new Location(lat, longitude),
                Address = address,
                Description = description
            });
            */

            //Add the PinViewModel (look below) to the pins list to display on the map
            pins.Add(new PinViewModel
            {
                Location = new Location(lat, longitude),
                Address = address,
                Description = description
            });
        }
    }

    /*public class vModelMap
    {
        public ObservableCollection<PinViewModel> Pins { get; set; }

        public vModelMap()
        {
            Pins = new ObservableCollection<PinViewModel>();
        }
    }*/

    public class PinViewModel
    {
        public Location Location { get; set; }
        public string Address { get; set; }
        public string Description { get; set; }
    }
}

PinDbContext.cs:

using System;
using Microsoft.EntityFrameworkCore;
using mauiMap.Models; 

namespace mauiMap
{
    public class PinDbContext:DbContext
    {

            public DbSet<Pin> Pins { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlite("Data Source=pin.db");
            }
    }
}
user4157124
  • 2,809
  • 13
  • 27
  • 42
  • 1
    please [edit] your question to include all the relevant information that you were adding in comments – Jason Jul 02 '23 at 18:25
  • You can refer to this case about [Entity Framework Core in .NET MAUI/Xamarin.Forms](https://stackoverflow.com/questions/72598734/entity-framework-core-in-net-maui-xamarin-forms). It seems you should give it a path. – Liyun Zhang - MSFT Jul 10 '23 at 09:45

0 Answers0