保存多个对象
我有以下课程:
public class Test
{
public int Id { get; set; }
public string Title { get; set; }
public List<TestQuestion> Questions { get; set; }
}
public class TestQuestion
{
public int Id { get; set; }
public int TestId { get; set; }
public string Text { get; set; }
public List<TestQuestionAnswer> Answers { get; set; }
}
public class TestQuestionAnswer
{
public int Id { get; set; }
public int TestQuestionId { get; set; }
public string Text { get; set; }
public bool? IsCorrect { get; set; }
}
我在 TestRepository
中的 Save
方法遇到一些问题。
逻辑如下:
If Test.Id > 0 then update Test, otherwise create a new one
If TestQuestion.Id > 0 and TestQuestion.Text = "" delete TestQuestion from database and all Answers for that object
If TestQuestion.Id == 0 then create a new row in database
If TestQuestion.Id > 0 and TestQuestion.Text != "" then update that row
If TestQuestionAnswer.Id > 0 and Text = "" then delete it from database, otherwise call create or update method.
我正在使用实体框架代码优先,但如果这会使这项工作变得更容易,我愿意切换到经典的 ADO.NET。
任何帮助将不胜感激!
I have the following classes:
public class Test
{
public int Id { get; set; }
public string Title { get; set; }
public List<TestQuestion> Questions { get; set; }
}
public class TestQuestion
{
public int Id { get; set; }
public int TestId { get; set; }
public string Text { get; set; }
public List<TestQuestionAnswer> Answers { get; set; }
}
public class TestQuestionAnswer
{
public int Id { get; set; }
public int TestQuestionId { get; set; }
public string Text { get; set; }
public bool? IsCorrect { get; set; }
}
I'm having some problems with Save
method in TestRepository
.
Here's the logic:
If Test.Id > 0 then update Test, otherwise create a new one
If TestQuestion.Id > 0 and TestQuestion.Text = "" delete TestQuestion from database and all Answers for that object
If TestQuestion.Id == 0 then create a new row in database
If TestQuestion.Id > 0 and TestQuestion.Text != "" then update that row
If TestQuestionAnswer.Id > 0 and Text = "" then delete it from database, otherwise call create or update method.
I'm using Entity Framework Code First, but I'm willing to switch to classic ADO.NET if that would make this job much easier.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于更新的明显复杂性,将其逻辑移动到存储过程可能是有意义的,然后 将您的更新映射到该存储过程。
Given the apparent complexity of your update, it would probably make sense to move its logic to a stored procedure, and then map your updates to that stored procedure.
有两种方法可以让这个变得简单,正如您所说,您想切换到经典的 ADO.NET,
创建一个存储过程并将所有这些逻辑放在其中。这可以在单个事务中完成。
编写带有相关查询的 ADO.NET 编码。
我个人更喜欢第一个选项,因为它将利用现有的实体框架。
让我知道进一步的方向,也让我知道在 TestRepository 中使用 Save 方法时出现的问题。
There are two ways you can make this simple and as you said, you would like to switch to classic ADO.NET,
Create a Stored Procedure and place all of these logic in there. This can be done in a single Transction.
Write the ADO.NET coding with related QUERY's.
I personally prefer first option since that will make use of the existing Entity Framework.
Let me know further direction and also may I know the issues while using Save method in TestRepository.