0

の新しいエンティティをSessionImages追加してから別のエンティティを追加すると (アップロードして)、問題が発生します。2 倍になります。例えば ​​:

  1. 画像の追加: ここに画像の説明を入力

  2. 後で別のものを追加する: ここに画像の説明を入力

これが私のコントローラーコードです:

[HttpPost]
public ActionResult SessionImages(FormCollection collection)
{
    int SessionID = Convert.ToInt32(collection[0]);
    Sessions SessionModel = sessionsRepo.GetSessionById(SessionID);
    bool uploadSucceded = Utility.Utility.UploadImages(this, Request.Files, Server.MapPath(Path.Combine("~/Photos/Sessions", SessionModel.Name)));
    for (int i = 0; i < Request.Files.Count; i++)
    {
        if (Request.Files[i].ContentLength == 0)
        {
            continue;
        }

        SessionImages ImageModel = new SessionImages
        {
            Name = Request.Files[i].FileName,
            Path = Path.Combine("~/Photos/Sessions/", SessionModel.Name, "actual", Request.Files[i].FileName),
            Session = SessionModel,
            SessionID = SessionID
        };
        sessionImagesRepo.Add(SessionModel, ImageModel);
    }            
    return RedirectToAction("SessionImages",SessionModel.ID);
}

sessionImagesRepo.Addメソッドは次のとおりです。

public SessionImages Add(Sessions SessionModel, SessionImages ImageModel)
{
    try
    {
        context.Entry(ImageModel).State = System.Data.EntityState.Added;
        SessionModel.PhotosCount = SessionModel.Images.Count;
        context.Entry(SessionModel).State = System.Data.EntityState.Modified;                
        context.SaveChanges();
    }
    catch
    {
        return null;
    }
    return ImageModel;
}

ここに私のエンティティがあります:

namespace FP.Domain.Entities
{
    public class Sessions
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime Date { get; set; }
        public int PhotosCount { get; set; }
        public string CoverPhoto { get; set; }
        public List<SessionImages> Images { get; set; }
    }
}


namespace FP.Domain.Entities
{
    public class SessionImages
    {
        public int ImageID { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public int SessionID { get; set; }
        public Sessions Session { get; set; }
    }
}

構成は次のとおりです。

namespace FP.Domain.Configurations
{
    public class SessionsConfig : EntityTypeConfiguration<Sessions>
    {
        public SessionsConfig()
        {
            ToTable("Sessions");
            Property(p => p.Name).IsRequired();
            Property(p => p.PhotosCount).IsRequired();
            Property(p => p.CoverPhoto).IsRequired();
            Property(p => p.Date).HasColumnType("date");
        }
    }
}


namespace FP.Domain.Configurations
{
    public class SessionImagesConfig : EntityTypeConfiguration<SessionImages>
    {
        public SessionImagesConfig()
        {
            ToTable("SessionImages");
            HasKey(e => e.ImageID);
            Property(p => p.Path).IsRequired();
            HasRequired(i => i.Session)
                .WithMany(s => s.Images)
                .HasForeignKey(i => i.SessionID);
        }
    }
}
4

2 に答える 2

0

回避策があれば、これらの行を削除することをお勧めします(ただし、ポイントはわかりますcount)...

context.Entry(ImageModel).State = System.Data.EntityState.Added;
SessionModel.PhotosCount = SessionModel.Images.Count;
context.Entry(SessionModel).State = System.Data.EntityState.Modified;                

それがおそらくそれを台無しにしているのです。私は似たような問題を何度も見てきました - そして、私は通常、まったく逆のことをしてしまいます:

context.Entry(Parent).State = System.Data.EntityState.Unchanged;  

あなたがそこでしていることはすべて、機能することですCount

最適化の理由は理解できますが、コレクションには本質的にその数があります。ロードなどしたくない場合は、これがより高速であることを知っています。

何を提案すればよいかわかりませんが、それが「犯人」であると思われます。

それなしで試してみて、テスト中はオフにしてカウントを無視してください。何が起こるか見てください。だとしたら、もう少し整理してみようかな。

于 2013-04-09T00:33:59.380 に答える