简体   繁体   中英

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

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.

I don't recognize this exception, but you better do this differently anyway.

The line

db.Presents.Add(obj);

...marks obj as added, but also each PresentPhoto in obj.PresentPhotos . That's just the way EF works. This means that the only thing that's left to do now is remove the Added status from the photos that aren't new:

foreach(var photo in obj.PresentPhotos
                        .Where(p => p.Id > 0)
                        .ToList())
{
    db.Entry(photo).State = EntityState.Detached; // or Unchanged
}

Now SaveChanges will save the new Present and new photos in one go. You don't need the transaction management anymore.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM