.net - 如何明智地将 Survey 对象与现有 Person 对象相关联,避免 ASP .NET MVC 5 Web 应用程序中 Entity Framework 中的冗余

标签 .net asp.net-mvc database entity-framework asp.net-mvc-5.1

我有一个 class Person 我想将 class Survey 的对象与它相关联。当然 SurveyQuestion 对象组成,QuestionAnswer 对象组成。

问题是,例如,如果我在数据库中有 10000 个 Persons(tops),并且每个人都将填写exacly same 调查,我最终可能会得到9999 个冗余的 QuestionSurvey 对象,而不是仅仅记住答案。

根据本教程:http://www.techiesweb.net/radio-button-list-in-asp-net-mvc/

我做过这样的事情:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.Models {
    public class Person {
        public int Id { get; set; }
        [DisplayName("First Name")]
        public string FirstName { get; set; }
        [DisplayName("Last Name")]
        public string LastName { get; set; }
        [DisplayName("Surveys")]
        public virtual ICollection<Survey> Surveys { get; set; }

    }
}

和调查部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models {
    public class Question {
        public int Id { set; get; }
        public string QuestionText { set; get; }
        public virtual  ICollection<Answer> Answers { set; get; }
        public string SelectedAnswer { set; get; }
        public virtual Survey Survey { get; set; }


    }
    public class Answer {
        public int Id { set; get; }
        public string AnswerText { set; get; }
        public virtual Question Question { get; set; }
    }
    public class Survey {
        public int Id { set; get; }
        public virtual ICollection<Question> Questions { set; get; }

        public virtual Person Person { get; set; }

    }
}

但我觉得这最终可能会产生大量不必要的数据。

如何解决这个问题,以避免在为每个用户初始化这些调查时使用大量代码和浪费内存。

编辑: 当前代码版本:

模型/调查.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models {
    public class Question {
        public int Id { set; get; }
        public string QuestionText { set; get; }
        public virtual ICollection<Answer> Answers { set; get; }
        public virtual Survey Survey { get; set; }
        public string SelectedAnswer { set; get; } //this field is SET after clicking SAVE SURVEY button
        /*    public Question() {
                Answers = new List<Answer>();
            }*/
    }
    public class Answer {
        public int Id { set; get; }
        public string AnswerText { set; get; } 
        public virtual Question Question { get; set; }
        public virtual ICollection<Person> Persons { get; set; }
    }
    public class Survey {
        public int Id { set; get; }
        public virtual ICollection<Question> Questions { set; get; }

        /*  public Survey() {
              Questions = new List<Question>();
          }*/
    }
}

模型/Person.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.Models {
    public class Person {
        public int Id { get; set; }
        [DisplayName("First Name")]
        public string FirstName { get; set; }
        [DisplayName("Last Name")]
        public string LastName { get; set; }
        [DisplayName("Cell Number")]
        public string CellNumber { get; set; }
        [DisplayName("Secondary Number")]
        public string SecondaryPhoneNumber { get; set; }
        [DisplayName("Address")]
        public string Address { get; set; }

        [DisplayName("Date of Birth")]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime BirthDate { get; set; }

        [DisplayName("Pesel")]
        public string Pesel { get; set; }


        [DataType(DataType.MultilineText)]
        [DisplayName("Notes")]
        public string Notes { get; set; }
        public virtual ICollection<Meeting> Meetings { get; set; }
        public int? StatusId { get; set; }
        public virtual Status Status { get; set; }
        [DisplayName("Agreed to process personal data processing")]
        public bool PersonalDataProcessing { get; set; }
        public virtual ICollection<Answer> Answers { get; set; }

    }
}

显示所有调查的 View _Survey1.cshtml(Details.html 的部分 View ):

@using WebApplication2.Models
@model   System.Tuple<Person, List<Survey>>

<hr />
<h1>Surveys</h1>
<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
@*<p>
        Number of Surveys: @Html.DisplayFor(x => Model.Item2.Count)
    </p>*@

@{int i = 1;}
@foreach (var survey in Model.Item2) {
    using (Html.BeginForm()) {
        <h2>Survey @(i)</h2>
        <p />
        @Html.EditorFor(x => survey.Questions)
        <button class='mybutton' type='button'  onclick="javascript:SubmitClick(@Model.Item1.Id, @survey.Id.);" >Click Me</button>
    }
    i++;
    <hr style="background-color:rgb(126, 126, 126);height: 5px" />
}
<hr />

EditorTemplates/Question.cshtml

@model WebApplication2.Models.Question
<div>
    @Html.HiddenFor(x => x.Id)
    <h3> @Model.QuestionText </h3>
    @foreach (var a in Model.Answers) {
        <p>
            @Html.RadioButtonFor(b => b.SelectedAnswer, a.Id)  @a.AnswerText
        </p>
    }
</div>

_Survey.cshtml View 如下所示:

enter image description here

当我单击“保存调查”按钮时,我调用了 Controllers/PersonController.cs 中的方法:

   public void SubmitSurvey(int personId, int surveyId) {
            //getting last selected answers from database and adding them to person
            //----PSEUDO CODE---- just to demonstrate
            Person person = db.findById(personId);
            List<Question> list = dbFindById(surveyId).Questions;
            person.Answers.Add(list);
            //---PSEUDO CODE----
            System.Diagnostics.Debug.WriteLine("UPDATING DATABASE");

        }

假设将选定的答案添加到 Person。

在点击保存调查之前选择的答案将被记住在字段 public string SelectedAnswer { set;得到; }//在 class Question 中单击 SAVE 按钮 后设置此字段,这在 EditorTemplates.Question.cshtml 中说明。

问题:我担心的是:如果 2 个用户(在不同的 PC 上)将打开 2 个人(不同的)并尝试同时提交相同的调查(数据库中只有 2 个调查并且每个问题中只有一个字段 SelectedAnswer)他们的请求会在服务器端相互交叉吗?

例如,相同的调查结果会被保存给两个人吗?或者他们的答案会混淆?

最佳答案

于是根据聊天中的对话,重构如下:

删除Person之间的引用和 Survey

添加ICollection<Person> PeopleAnswer , 添加 ICollection<Answer> AnswersPerson

使用Entity Framework Fluent APIPerson 之间创建多对多关系和 Answer如下:

modelbuilder.Entity<Person>().HasMany(p => p.Answers).WithMany(a => a.People)

关于.net - 如何明智地将 Survey 对象与现有 Person 对象相关联,避免 ASP .NET MVC 5 Web 应用程序中 Entity Framework 中的冗余,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474627/

相关文章:

c# - 具有自动递增文件版本的固定程序集版本?

c# - WebBrowser 控件和 Wininet API

asp.net-mvc - 是否必须在 mvc 中命名 View 和操作方法相同?

定期数据库批量插入的Java并发

java - 使用数据库池时出现太多连接错误

php - 如何使用 Travis CI 测试 laravel 5.2 项目

c# - 如何在 HTTP POST 中获取 List<Model> 对象

.net - WCF - 进行多次调用时随机客户端超时

mysql - 如何在两个表中建立外键关系

asp.net-mvc - MVC .net 属性上必需属性的 bool 值 True