Entity Framework 6 One To Many Relationship Add and Update Results Exceptions

c# entity-framework entity-framework-6

Question

i have two classes :

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; }
}

and

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; }

}

i am trying to add a present object with photos to db like :

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;
                }
            }

this is always results with

Cannot apply indexing with [] to an expression of type System.Collections.Generic.IEnumerable

validation error at seconds db.saveChanges() method. and i try to update present and photos i am getting the same error.

1
0
2/21/2015 7:44:07 PM

Popular Answer

the error was a max length exception inside. but the message was that. i dont know why we cant access a valiadtion exception inside that. and get values like validationErrorType, error class, error field etc.

they are writing a very big project. but they're hiding all exceptions. i can't believe this amateurism.

0
2/21/2015 9:42:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow