.NET Maui, Inserting Entitys in SQL Database, PK Field int, Starting from int value 0 causes crash
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
# Error:
```
I/DOTNET : Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
I/DOTNET : ---> System.InvalidOperationException: The instance of entity type 'Adressen' cannot be tracked because another instance with the key value '{Id: 1}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
```
# Code used:
```C#
Adressen:
------------
using Microsoft.EntityFrameworkCore;
namespace MauiApp1;
[PrimaryKey(nameof(Id))]
public class Adressen
{
public int Id { get; set; }
public string nachname { get; set; }
public string vorname { get; set; }
public string strasse { get; set; }
public string plz { get; set; }
public string ort { get; set; }
}
DBContextApp.cs:
------------
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp1;
public class DbContextApp : DbContext
{
public DbContextApp()
{
// Get call stack
StackTrace stackTrace = new StackTrace();
// Get calling method name
System.Diagnostics.Debug.WriteLine((stackTrace.GetFrame(1).GetMethod().Name));
try
{
//Database.EnsureCreatedAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(
"Error DbContextApp/Database.EnsureCreated: " + ex.Message.ToString()
);
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dbPath = "mbcapphek.db";
#if IOS
if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
{
dbPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
dbPath
);
//SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
SQLitePCL.Batteries_V2.Init();
}
#endif
#if ANDROID
if (DeviceInfo.Current.Platform == DevicePlatform.Android)
{
dbPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
dbPath
);
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
}
#endif
optionsBuilder.UseSqlite($"Data Source={dbPath}");
optionsBuilder.EnableSensitiveDataLogging();
}
//}
public DbSet Adressen { get; set; }
}
MainPage.xaml.xs:
--------------------
namespace MauiApp1;
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
using (DbContextApp db = new DbContextApp())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
try
{
for (int i = 0; i < 1000; i++)
{
Adressen oObject = new Adressen();
oObject.Id = i;
oObject.nachname = "Meier" + i.ToString();
oObject.vorname = "Markus" + i.ToString();
oObject.strasse = "Meierplatz" + i.ToString();
oObject.plz = "33100" + i.ToString();
oObject.ort = "Paderborn" + i.ToString();
db.Adressen.Add(oObject);
oObject = null;
}
db.SaveChanges();
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
var listeEingefuegt = db.Adressen;
foreach (Adressen oAdresse in listeEingefuegt.ToList())
{
Console.WriteLine(
"oSort.Id -> "
+ oAdresse.Id
+ "/"
+ oAdresse.nachname
+ "/"
+ oAdresse.vorname
+ "/"
+ oAdresse.strasse
+ "/"
+ oAdresse.plz
+ "/"
+ oAdresse.ort
);
}
}
}
```
# Packets/Nugets:
dotnet list package >nuget-list.txt
```
Das Projekt "MauiApp1" enthält die folgenden Paketverweise.
[net7.0-android33.0]:
Paket oberster Ebene Angefordert Aufgelöst
> Microsoft.EntityFrameworkCore 7.0.13 7.0.13
> Microsoft.EntityFrameworkCore.Sqlite.Core 7.0.13 7.0.13
> Microsoft.Extensions.Configuration (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.DependencyInjection (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.Logging (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.Logging.Debug 7.0.0 7.0.0
> Microsoft.Maui.Graphics (A) [7.0.96, ) 7.0.96
> sqlite-net-pcl 1.8.116 1.8.116
> SQLitePCLRaw.bundle_green 2.1.6 2.1.6
> Xamarin.Android.Glide (A) [4.13.2.2, ) 4.13.2.2
> Xamarin.AndroidX.Browser (A) [1.4.0.3, ) 1.4.0.3
> Xamarin.AndroidX.Legacy.Support.V4 (A) [1.0.0.15, ) 1.0.0.15
> Xamarin.AndroidX.Lifecycle.LiveData (A) [2.5.1.1, ) 2.5.1.1
> Xamarin.AndroidX.Navigation.Common (A) [2.5.2.1, ) 2.5.2.1
> Xamarin.AndroidX.Navigation.Fragment (A) [2.5.2.1, ) 2.5.2.1
> Xamarin.AndroidX.Navigation.Runtime (A) [2.5.2.1, ) 2.5.2.1
> Xamarin.AndroidX.Navigation.UI (A) [2.5.2.1, ) 2.5.2.1
> Xamarin.AndroidX.Security.SecurityCrypto (A) [1.1.0-alpha03, ) 1.1.0-alpha03
> Xamarin.Google.Android.Material (A) [1.7.0, ) 1.7.0
> Xamarin.Google.Crypto.Tink.Android (A) [1.7.0.1, ) 1.7.0.1
[net7.0-ios16.1]:
Paket oberster Ebene Angefordert Aufgelöst
> Microsoft.EntityFrameworkCore 7.0.13 7.0.13
> Microsoft.EntityFrameworkCore.Sqlite.Core 7.0.13 7.0.13
> Microsoft.Extensions.Configuration (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.DependencyInjection (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.Logging (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.Logging.Debug 7.0.0 7.0.0
> Microsoft.Maui.Graphics (A) [7.0.96, ) 7.0.96
> sqlite-net-pcl 1.8.116 1.8.116
> SQLitePCLRaw.bundle_green 2.1.6 2.1.6
[net7.0-maccatalyst16.1]:
Paket oberster Ebene Angefordert Aufgelöst
> Microsoft.EntityFrameworkCore 7.0.13 7.0.13
> Microsoft.EntityFrameworkCore.Sqlite.Core 7.0.13 7.0.13
> Microsoft.Extensions.Configuration (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.DependencyInjection (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.Logging (A) [7.0.0, ) 7.0.0
> Microsoft.Extensions.Logging.Debug 7.0.0 7.0.0
> Microsoft.Maui.Graphics (A) [7.0.96, ) 7.0.96
> sqlite-net-pcl 1.8.116 1.8.116
> SQLitePCLRaw.bundle_green 2.1.6 2.1.6
(A): Automatisch referenziertes Paket.
```
Contributor guide
Assessment
This issue has not been assessed yet.