【问题标题】:Records are doubled when adding referenced entities using Entity Framework使用实体框架添加引用实体时记录加倍
【发布时间】:2013-04-08 15:14:22
【问题描述】:

我有一个问题,当我添加一个SessionImages 的新实体,然后添加另一个(通过上传)时,它会翻倍。 例如:

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

【问题讨论】:

    标签: c# asp.net sql asp.net-mvc entity-framework


    【解决方案1】:

    尝试像这样更改存储库方法:

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

    并在操作中删除我评论过的行:

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

    【讨论】:

      【解决方案2】:

      我建议你放弃这些行,如果你能解决它(我可以在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 正常工作。

      我可以理解优化的原因,但你在集合中确实有这个数量。我知道如果您不想加载等,这会更快。

      我不确定该建议什么 - 但我怀疑这是“罪魁祸首”。

      尝试不使用它,在测试时关闭并忽略计数。看什么 发生。如果是这样,那就考虑如何重新组织一下。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多