【发布时间】:2014-09-04 09:46:56
【问题描述】:
我有三个表 QuestionBank、Question 和 Answer。 “ QuestionBank ”将有问题列表,“ Question ”将有“ Answer ”列表。
题库:-
public class QuestionBank { public int id { get; set; } public string Text { get; set; } public string Chapter { get; set; } public string Standard { get; set; } public List<Question> Question { get; set; } public QuestionBank() { this.Question = new List<Question>(); } }
问题:-
public class Question { public int id { get; set; } public string Title { get; set; } public string QuestionText { get; set; } public List<Answer> Answer { get; set; } public string CorrectAnswer { get; set; } public Question() { this.Answer = new List<Answer>(); } }
答案:-
public class Answer { public int id { get; set; } public string AnswerText { get; set; } }
WEB API :- //已编辑
private IRepository<QuestionBank> _QuestionBankRepository; public QuestionController(IRepository<QuestionBank> QuestionBankRepository) { _QuestionBankRepository = QuestionBankRepository; } [HttpPost] [Route("Ques/Add")] public Boolean Add(QuestionBank AddQuetionBankData) { var isQuetionBankPresent = _QuestionBankRepository.GetAll(p => p.Text == AddQuetionBankData.Text && p.Standard == AddQuetionBankData.Standard && p.Chapter == AddQuetionBankData.Chapter).FirstOrDefault<QuestionBank>(); if (isQuetionBankPresent != null) { /* Add the data in Question and Answer tables */ return false; } else { /* Add the data in all three tables */ return true; } }
我有这个用于 web api 的数据库。现在我想通过 json { "QuestionBank": QuestionBank, "Question": Question, "Answer": Answer } 添加数据库中的数据,如果 QuestionBank 中存在该行我不想在 QuestionBank 表中添加该数据,只添加问答表中的数据与各自的外键。我正在使用实体框架和 mvc 5 web api。我被困在这一点上。如果需要任何东西,请告诉我。提前致谢。
【问题讨论】:
标签: entity-framework asp.net-mvc-5 entity-framework-6 asp.net-web-api2