在插入父对象和子对象之前添加子对象

发布于 2024-11-27 15:57:01 字数 2644 浏览 1 评论 0原文

我完成了Nerddinner应用程序。在 Create 操作方法中,它们具有以下代码:

NerdIdentity nerd = (NerdIdentity)User.Identity;
dinner.HostedById = nerd.Name;
dinner.HostedBy = nerd.FriendlyName;

RSVP rsvp = new RSVP();
rsvp.AttendeeNameId = nerd.Name;
rsvp.AttendeeName = nerd.FriendlyName;
dinner.RSVPs.Add(rsvp);

dinnerRepository.Add(dinner);
dinnerRepository.Save();

我首先使用 Entity Framework 4.1 代码。

这是我的 GrantApplication 类:

public class GrantApplication
{
   public int Id { get; set; }
   // Other properties
   public virtual ICollection<AuditEntry> AuditEntries { get; set; }
}

在我的服务层中,我执行以下操作,与 Nerddinner 所做的相同:

public void Insert(GrantApplication grantApplication)
{
   // Add audit entry
   grantApplication.AuditEntries.Add(new AuditEntry
   {
      NewValue = grantApplication.GrantApplicationStateId,
      AuditDate = currentDateTime,
      EmployeeNumber = submitterEmployeeNumber
   });

   // Insert the new grant application
   grantApplicationRepository.Insert(grantApplication);
}

我的 AuditEntry 类:

public class AuditEntry
{
   public int Id { get; set; }
   public int OldValue { get; set; }
   public int NewValue { get; set; }
   public DateTime AuditDate { get; set; }
   public string EmployeeNumber { get; set; }
}

我的上下文类:

public class HbfContext : DbContext
{
   public DbSet<Bank> Banks { get; set; }
   public DbSet<AccountType> AccountTypes { get; set; }
   public DbSet<GrantApplication> GrantApplications { get; set; }
   public DbSet<AuditEntry> AuditEntries { get; set; }

   protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
   {
   }
}

我收到一个错误,grantApplication.AuditEntries 为 null,所以它无法添加审核条目对象。为什么我的为空,但晚餐.RSVPs 在尝试添加 RSVP 对象时不为空?我该如何解决它?

我需要将 AuditEntries 添加到 HbfContext 吗?我的意思是我不会单独使用它。仅当编辑 GrantApplication 时才会使用它。

更新

我一定使用的是旧版本的 Nerddinner,但这就是我的 Create 的样子:

[HttpPost, Authorize]
public ActionResult Create(Dinner dinner)
{
   if (ModelState.IsValid)
   {
      NerdIdentity nerd = (NerdIdentity)User.Identity;
      dinner.HostedById = nerd.Name;
      dinner.HostedBy = nerd.FriendlyName;

      RSVP rsvp = new RSVP();
      rsvp.AttendeeNameId = nerd.Name;
      rsvp.AttendeeName = nerd.FriendlyName;
      dinner.RSVPs.Add(rsvp);

      dinnerRepository.Add(dinner);
      dinnerRepository.Save();

      return RedirectToAction("Details", new { id=dinner.DinnerID });
   }

   return View(dinner);
}

I worked through the Nerd Dinner application. In the Create action method they have the following code:

NerdIdentity nerd = (NerdIdentity)User.Identity;
dinner.HostedById = nerd.Name;
dinner.HostedBy = nerd.FriendlyName;

RSVP rsvp = new RSVP();
rsvp.AttendeeNameId = nerd.Name;
rsvp.AttendeeName = nerd.FriendlyName;
dinner.RSVPs.Add(rsvp);

dinnerRepository.Add(dinner);
dinnerRepository.Save();

I am using Entity Framework 4.1 code first.

Here is my GrantApplication class:

public class GrantApplication
{
   public int Id { get; set; }
   // Other properties
   public virtual ICollection<AuditEntry> AuditEntries { get; set; }
}

In my service layer I do the following, the same as what Nerd Dinner does it:

public void Insert(GrantApplication grantApplication)
{
   // Add audit entry
   grantApplication.AuditEntries.Add(new AuditEntry
   {
      NewValue = grantApplication.GrantApplicationStateId,
      AuditDate = currentDateTime,
      EmployeeNumber = submitterEmployeeNumber
   });

   // Insert the new grant application
   grantApplicationRepository.Insert(grantApplication);
}

My AuditEntry class:

public class AuditEntry
{
   public int Id { get; set; }
   public int OldValue { get; set; }
   public int NewValue { get; set; }
   public DateTime AuditDate { get; set; }
   public string EmployeeNumber { get; set; }
}

My context class:

public class HbfContext : DbContext
{
   public DbSet<Bank> Banks { get; set; }
   public DbSet<AccountType> AccountTypes { get; set; }
   public DbSet<GrantApplication> GrantApplications { get; set; }
   public DbSet<AuditEntry> AuditEntries { get; set; }

   protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
   {
   }
}

I get an error that grantApplication.AuditEntries is null so it can't add the audit entry object. Why is mine null, but dinner.RSVPs is not null when it tries to add the RSVP obkect? How would I fix it?

Do I need to add AuditEntries to HbfContext? I mean I'm not going to use it on it's own. It will only be used when a GrantApplication is edited.

UPDATE

I must be using an older version of Nerd Dinner, but this is what my Create looks like:

[HttpPost, Authorize]
public ActionResult Create(Dinner dinner)
{
   if (ModelState.IsValid)
   {
      NerdIdentity nerd = (NerdIdentity)User.Identity;
      dinner.HostedById = nerd.Name;
      dinner.HostedBy = nerd.FriendlyName;

      RSVP rsvp = new RSVP();
      rsvp.AttendeeNameId = nerd.Name;
      rsvp.AttendeeName = nerd.FriendlyName;
      dinner.RSVPs.Add(rsvp);

      dinnerRepository.Add(dinner);
      dinnerRepository.Save();

      return RedirectToAction("Details", new { id=dinner.DinnerID });
   }

   return View(dinner);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2024-12-04 15:57:01

嗯,因为 NerdDinner DiningsController.Create 包含一行您没有显示的代码

            dinner.RSVPs = new List<RSVP>(); // why is this not in your example?
            dinner.RSVPs.Add(rsvp);

我需要将 AuditEntries 添加到 HbfContext 吗?

是的,你知道。那么,您必须以某种方式将它们添加到您的 EF 模型中。这是一种方法。您也可以使用 OnModelCreating 中的代码来完成此操作。

Um, because the NerdDinner DinnersController.Create includes a line of code which you didn't show?

            dinner.RSVPs = new List<RSVP>(); // why is this not in your example?
            dinner.RSVPs.Add(rsvp);

Do I need to add AuditEntries to HbfContext?

Yes, you do. Well, you have to add them to your EF model in some way. That's one way to do it. You might be able to do it with code in OnModelCreating as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文