【问题标题】:How can I add a record with a foreign key using entity framework?如何使用实体框架添加具有外键的记录?
【发布时间】:2011-12-13 20:55:13
【问题描述】:

我正在尝试通过像这样的 XML 文件将记录添加到我的数据库中:

<Answers ExamID="1" StudentID="abcd" Date="10/26/2011 11:50:34 AM" Seed="495"
      IsSED="False">
  <Summary>
    <Objective ID="1" MakeUp="False" Quantify="5" Difficulty="Easy"
         Accredited="True" Produced="True">
      <Details Result="0" Date="10/26/2011 11:35:18 AM" />
      <Details Result="1" Date="10/26/2011 11:50:34 AM" />
    </Objective>
    <Objective ID="2" MakeUp="True" Quantify="5" Difficulty="Easy"
         Accredited="False" Produced="True">
      <Details Result="0" Date="10/26/2011 11:35:18 AM" />
      <Details Result="0" Date="10/26/2011 11:50:34 AM" />
    </Objective>
  </Summary>
  <Answer ProblemID="0" ObjectiveID="1" IsCorrect="True" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="9" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="1" IsCorrect="True" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="20" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="1" IsCorrect="True" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="16" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="1" IsCorrect="True" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="36" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="1" IsCorrect="True" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="18" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="2" IsCorrect="False" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="Null" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="2" IsCorrect="False" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="Null" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="2" IsCorrect="False" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="Null" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="2" IsCorrect="False" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="Null" />
  </Answer>
  <Answer ProblemID="0" ObjectiveID="2" IsCorrect="False" Difficulty="Easy">
    <Result DataType="System.Decimal" Value="Null" />
  </Answer>
</Answers>

请检查图表。我需要获取一些数据以将记录添加到 EXAM PRODUCED、EXERCISE 和 ANSWER 表(实体)。

在 EXAM PRODUCED 中,我有一个名为 ExamID hide 的字段。我的主要问题是,如何将当前文件引用到考试中?

你认为我需要创建一个存储过程并且 SQL SERVER 必须这样做吗? 还是使用 Linq to SQL?

我对这种情况有点困惑。

【问题讨论】:

  • Registries = 记录,我假设。

标签: sql linq sql-server-2008 entity-framework entity-framework-4.1


【解决方案1】:

您可以将ExamProduced 实例添加到ExamExamsProduced 集合中。 DbContext 将在插入时设置 ExamId。

【讨论】:

  • ExamProduced 实例应该添加到 ExamsProduced 表中吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
  • 2019-09-25
相关资源
最近更新 更多