Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 MVC4模型属性应该是另一个类的列表或ICollection_Asp.net Mvc 4_Ef Code First - Fatal编程技术网

Asp.net mvc 4 MVC4模型属性应该是另一个类的列表或ICollection

Asp.net mvc 4 MVC4模型属性应该是另一个类的列表或ICollection,asp.net-mvc-4,ef-code-first,Asp.net Mvc 4,Ef Code First,我首先使用实体框架代码创建一种测试生成应用程序。我有一个名为Question的基类,它是multipleechoicequestion、EssayQuestion和其他问题类型的派生类多个回声问题显然有多个答案供考生选择。我的问题是选择在问题实例中存储它们的最佳方式 我可以用一个字符串列表来声明类以保存答案,如下所示: public class MulitpleChoiceQuestion : Question { private List<String> Answers =

我首先使用实体框架代码创建一种测试生成应用程序。我有一个名为
Question
的基类,它是
multipleechoicequestion
EssayQuestion
和其他问题类型的派生类<代码>多个回声问题显然有多个答案供考生选择。我的问题是选择在问题实例中存储它们的最佳方式

我可以用一个字符串列表来声明类以保存答案,如下所示:

public class MulitpleChoiceQuestion : Question
{
    private List<String> Answers = new List<String>();
    // other declarations, etc.
}
然后在我的问题子类中(不仅仅是
multipleechoicequestions

公共类多选项问题:问题
{
公共虚拟ICollection答案{get;set;}
//其他声明等。
}
有比这两种方法更好的方法吗?如果没有,哪一个更好,为什么?我很难在网上找到这么详细的东西,而且大多数书也没有深入到这么深。
提前感谢您的指导。

我问了我的一位.NET教授朋友这个问题,以下是他的回答:

您的两个声明都在调用集合。列表是一个键入的列表 ICollection未类型化时的集合。类型化集合(列表)已被删除 与非类型集合相比有两个优势。每个集合的类型 在编译时进行检查,从而防止运行时错误。第二 它们减少了检索时所需的投射量 对象

我首先实现了ICollection解决方案,它在几个地方都很笨重(例如,种子数据的初始值设定项):

var mcQuestions=新列表
{
新问题{
QuestionText=“表达式返回的值是什么(true==false?'yes':'no')?”,
答案=新列表{new AnswerText=“true”},新答案{AnswerText=“false”},新答案{AnswerText=“无法确定”},新答案{AnswerText=“45”},新答案{AnswerText=“blue”}
},
新问题{
QuestionText=“C-Sharp通过以下方式响应全局变量声明:”,
Answers=新列表{new AnswerText=“抛出编译错误”},新答案{AnswerText=“抛出运行时错误”},新答案{AnswerText=“抛出无效运算符警告”},新答案{AnswerText=“向控制台打印警告”},新答案{AnswerText=“不做任何事情;全局变量是合法的”}
}
};
ForEach(mcq=>context.MultipleChoiceQuestions.Add(mcq));
SaveChanges();
虽然这个解决方案可能更灵活,但我认为从长远来看,这个列表会更清晰,更易于维护。我想不出有什么理由把复杂性作为未来可能的灵活性的权衡。所以这是我的清单。 希望这能帮助其他人。 祝你好运,好代码。
J

我还没有尝试过类似的方法,但我希望EF能够将您的列表转换为数据库端的一个单独的答案表,因此我希望两种解决方案都会产生相同的数据库模型。无论如何,如果两种方法都有效,那么选择哪一种将是一个品味问题

就我个人而言,我会选择这个列表,因为它看起来是最简单的解决方案,简单通常更好。如果您希望您的类更好地表示您的数据库,那么这可能是创建单独答案类的一个原因。如果你希望在将来扩展你的答案,那可能是另一个选择一个独立答案类而不是简单列表的原因


一般来说,我会说:如果您有两种解决问题的方法,那么选择一种方法可以使您的代码在查看代码时最容易阅读/理解。

谢谢您的建议。我同意,在这种情况下越简单越好。
public class Answer
{
    public int AnswerID { get; set; }
    public String AnswerText { get; set; }

    public virtual Question Question { get; set; }
}
public class MulitpleChoiceQuestion : Question
{
    public virtual ICollection<Answer> Answers { get; set; }
    // other declarations, etc.
}
    var mcQuestions = new List<MultipleChoiceQuestion>
    {
        new MultipleChoiceQuestion { 
            QuestionText = "What is the value returned by the expression (true == false? 'yes': 'no')?",
            Answers = new List<Answer> { new Answer { AnswerText="true"}, new Answer { AnswerText = "false"}, new Answer { AnswerText = "can't be determined"}, new Answer {AnswerText = "45"}, new Answer { AnswerText = "blue"}}
        },
        new MultipleChoiceQuestion { 
            QuestionText = "C-Sharp responds to a global variable declaration by:", 
            Answers = new List<Answer> { new Answer { AnswerText="throwing a compile error"}, new Answer { AnswerText = "throwing a runtime error"}, new Answer { AnswerText = "Throwing an Invalid operator warning"}, new Answer {AnswerText = "Printing a warning to the console"}, new Answer { AnswerText = "doing nothing; global variables are legal"}}
        }
    };
    mcQuestions.ForEach(mcq => context.MultipleChoiceQuestions.Add(mcq));
    context.SaveChanges();