I am developing an App in Xamarin type PCL, for my DB structure, I want to use SQLite, but I have the following doubts ...
When entering a record in my DB, it takes the ID = 0, in my data model use
[PrimaryKey, AutoIncrement] Public int ViolationID {get; set; }
And still, I enter the registry at zero, I do not know I'm doing wrong ... or is this a bug in the SQLite.NET-PCL package?
How can I verify that these records are actually being entered? I have in my code
public class DataAccess : IDisposable { private SQLiteConnection connection; public DataAccess() { var entity = DependencyService.Get<IEntity>(); connection = new SQLiteConnection(entity.Plataforma, System.IO.Path.Combine(entity.DirectorioBD, "Infraccions.db3")); connection.CreateTable<Infraccion>(); } public void InsertInfraccion(Infraccion infraccion) { connection.Insert(infraccion); } public void UpdateInfraccion(Infraccion infraccion) { connection.Update(infraccion); } public Infraccion GetInfraccion(int InfraccionID) { return connection.Table<Infraccion>().FirstOrDefault(c => c.InfraccionID == InfraccionID); } public List<Infraccion> GetInfraccions() { return connection.Table<Infraccion>().OrderBy(c => c.MotivoID).ToList(); } public void DeleteInfraccion(Infraccion infraccion) { connection.Delete(infraccion); } public void Dispose() { connection.Dispose(); } }
Should I create a table called Infraccions.db3 on my phone?
Thank you for your comments...
Are you saying that all new records have an ID of 0 and overwrite the existing record in the database?
If so, then this is how SQLite works - for int columns 0 is a valid value, so when the ID is 0 it will overwrite the record, instead of incrementing the value.
The correct way to use int primary keys is to define your primary key as a nullable int, that way the value for a new record is null, which will be updated to the next available id:
Essentially change
int
toint?
.