ho due classi:
public class Present : ITripleStarModelBase
{
[Key]
public int Id { get; set; }
public virtual ICollection<PresentPhoto> PresentPhotos { get; set; }
[MaxLength(100)]
[Index(IsUnique = true)]
[Required]
[Changable]
public string Name { get; set; }
[MaxLength(500)]
[Changable]
public string Description { get; set; }
[Changable]
public float Points { get; set; }
public DateTime CreatedAt { get; set; }
[Changable]
public DateTime ModifiedAt { get; set; }
[Changable]
public bool IsActive { get; set; }
}
e
public class PresentPhoto :ITripleStarModelBase
{
[Key]
public int Id { get; set; }
//public int PresentId { get; set; }
//[ForeignKey("PresentId")]
//public virtual Present Present { get; set; }
public int PresentId { get; set; }
[ForeignKey("PresentId")]
public virtual Present Present { get; set; }
[MaxLength(50)]
[Required]
public string PhotoName { get; set; }
public int Size { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
}
Sto cercando di aggiungere un oggetto presente con foto a db come:
obj = RequestHelper.getRequestBody<Present>();
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
obj.IsActive = true;
db.Presents.Add(obj);
db.SaveChanges();
foreach (var item in obj.PresentPhotos)
{
if (item.Id == 0)
{
item.PresentId = obj.Id;
db.PresentPhotos.Add(item);
}
}
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception ex2)
{
dbContextTransaction.Rollback();
throw ex2;
}
}
questo è sempre risultato con
Impossibile applicare l'indicizzazione con [] a un'espressione di tipo System.Collections.Generic.IEnumerable
errore di convalida in secondi con il metodo db.saveChanges (). e cerco di aggiornare il presente e le foto sto ricevendo lo stesso errore.
l'errore era un'eccezione di lunghezza massima all'interno. ma il messaggio era quello. Non so perché non possiamo accedere a un'eccezione di valiadtion al suo interno. e ottieni valori come validationErrorType, classe di errore, campo errore ecc.
stanno scrivendo un progetto molto grande. ma nascondono tutte le eccezioni. non posso credere a questo dilettantismo.