It works if I send a List through, but to test I was sending an IEnumerable and the Ids were not set.
To test this:
public class Product {
private int _Id;
private DateTime _CreatedDate;
private DateTime _UpdatedDate;
private int _CreatedByPortalUserId;
private int _UpdatedByPortalUserId;
private string _Notes;
// SQL Type: int
[Alias("ProductId")]
[AutoIncrement]
[JsonProperty]
public int Id {
get => _Id;
set {
_Id = value;
}
}
// SQL Type: datetime2
[Required]
[JsonProperty]
public DateTime CreatedDate {
get {
return _CreatedDate;
}
set {
_CreatedDate = value;
}
}
// SQL Type: datetime2
[Required]
[JsonProperty]
public DateTime UpdatedDate {
get {
return _UpdatedDate;
}
set {
_UpdatedDate = value;
}
}
// SQL Type: int
[Required]
[JsonProperty]
public int CreatedByPortalUserId {
get {
return _CreatedByPortalUserId;
}
set {
_CreatedByPortalUserId = value;
}
}
// SQL Type: int
[Required]
[JsonProperty]
public int UpdatedByPortalUserId {
get {
return _UpdatedByPortalUserId;
}
set {
_UpdatedByPortalUserId = value;
}
}
// SQL Type: nvarchar
[Required]
[JsonProperty]
public string Notes {
get {
return _Notes;
}
set {
_Notes = value;
}
}
}
private IEnumerable<Product> GetProducts(int count = 5)
{
for (int i = 0; i < count; i++)
{
yield return new Product
{
CreatedDate = DateTime.Now,
Notes = $"{i}",
UpdatedDate = DateTime.Now,
CreatedByPortalUserId = 1,
UpdatedByPortalUserId = 1
};
}
}
var products = GetProducts().ToList(); // WITHOUT ToList() the Id is not set
await db.SaveAllAsync<Product>(products);
Debug.Assert(products.All(x => x.Id > 0));
Did you try to debug this? The instances are getting populated, but you’re not seeing them because you’re not looking at the same instances, you’re just re-enumerating the yielded sequence again, creating & inspecting new instances.