C# 使用实体框架添加引用实体时,记录会加倍

C# 使用实体框架添加引用实体时,记录会加倍,c#,asp.net,sql,asp.net-mvc,entity-framework,C#,Asp.net,Sql,Asp.net Mvc,Entity Framework,我有一个问题,当我添加一个新的SessionImages实体,然后添加另一个实体(通过上传),它会加倍。 例如: 添加图像: 之后添加另一个: 这是我的控制器代码: [HttpPost] public ActionResult SessionImages(FormCollection collection) { int SessionID = Convert.ToInt32(collection[0]); Sessions SessionModel = sessionsRep

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

  • 添加图像:

  • 之后添加另一个:

  • 这是我的控制器代码:

    [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);
    }
    
    以下是我的实体:

    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; }
        }
    }
    
    命名空间FP.Domain.Entities
    {
    公开课
    {
    公共int ID{get;set;}
    公共字符串名称{get;set;}
    公共字符串说明{get;set;}
    公共日期时间日期{get;set;}
    公共计数{get;set;}
    公共字符串CoverPhoto{get;set;}
    公共列表图像{get;set;}
    }
    }
    命名空间FP.Domain.Entities
    {
    公共类会话映像
    {
    公共int-ImageID{get;set;}
    公共字符串名称{get;set;}
    公共字符串路径{get;set;}
    public int SessionID{get;set;}
    公共会话会话{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);
            }
        }
    }
    
    命名空间FP.Domain.Configurations
    {
    公共类sessionconfig:EntityTypeConfiguration
    {
    公共会话配置()
    {
    ToTable(“会话”);
    属性(p=>p.Name).IsRequired();
    属性(p=>p.photoscont).IsRequired();
    属性(p=>p.CoverPhoto).IsRequired();
    属性(p=>p.Date)。HasColumnType(“日期”);
    }
    }
    }
    命名空间FP.Domain.Configurations
    {
    公共类SessionImageConfig:EntityTypeConfiguration
    {
    公共会话映像配置()
    {
    ToTable(“SessionImages”);
    HasKey(e=>e.ImageID);
    属性(p=>p.Path).IsRequired();
    HasRequired(i=>i.Session)
    .具有许多(s=>s.图像)
    .HasForeignKey(i=>i.SessionID);
    }
    }
    }
    
    尝试如下更改存储库方法:

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

    如果你能解决这个问题的话,我建议你把这些行删掉(不过我可以在
    计数
    中看到这一点)

    这可能就是问题所在。我不止一次地看到过类似的问题,然后我通常会做相反的事情:

    context.Entry(Parent).State = System.Data.EntityState.Unchanged;  
    
    您在那里所做的一切都是为了让
    计数
    正常工作

    我可以理解优化的原因,但您在集合中固有地拥有这一点。我知道如果你不想加载等,这会更快

    我不知道该建议什么——但我怀疑这是“罪魁祸首”

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

    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;