i have an UWP-Project collecting some data and i want to save them into a database. I created a .net Libary with EntityFrameworkcore sqlite.
im creating the DBContext:
public class DBContext : DbContext
{
public DbSet<MyModel> Model { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=MyModel.db");
}
}
And MyModel class Contains following code:
public class MyModel
{
[Key]
public int Id;}
Now im calling from my UWP-Project just the DatabaseContext to store my data:
public static class DatabaseService
{
public static void SaveToDb(DataList model)
{
using (var db = new DBContext())
{
foreach(var auction in model.auctions)
{
MyModel data = new MyModel() { Id = model.Id};
db.Model.Add(data);
}
db.SaveChanges();
}
}
}
But he still complains that MyModel doesent have a definition for a PrimaryKey.
Do i miss something that it is recognizing the Key Anotation for PrimaryKey?
The key needs to be a property, not a field.
public class MyModel
{
[Key]
public int Id {get;set;}
}