无法保存List <t>类成员

时间:2016-06-04 15:10:06

标签: c# asp.net-mvc database model

我正在尝试使用受邀者列表保存约会。 约会保存得很好,但保存被邀请者不起作用。

这就是我所拥有的:

预约模式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ArrangeAppointments.Interfaces;

namespace ArrangeAppointments.Models
{
    public class Appointment : IAppointment
    {
        #region Implementation of IAppointments
            public int Id { get; set; }
            public int UserId { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
            public string Duration { get; set; }
            public List<Invitee> Invitees { get; set; }
        #endregion

        public void Save()
        {
            int appID = 0;
            using (var db = new MainDbContext())
            {
                var appointment = db.Appointments.Create();
                appointment.UserId = 1;
                appointment.Title = Title;
                appointment.Description = Description;
                appointment.Duration = Duration;
                db.Appointments.Add(appointment);
                int newId = db.SaveChanges();
                appID = appointment.Id;
            }
            SaveInvitees(appID);
        }

        public bool SaveInvitees(int appId)
        {
            try
            {
                foreach (var invitee in Invitees)
                {
                    //invitee.Appointment_Id = appId;
                    invitee.Save(appId);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.InnerException);
                return false;
            }
            return true;

        }
    }
}

受邀者的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ArrangeAppointments.Interfaces;

namespace ArrangeAppointments.Models
{
    public class Invitee : IInvitee
    {
        public int Id { get; set; }
        //public int Appointment_Id { get; private set; }
        public string Email { get; set; }
        public string Presence { get; set; }

        public void Save(int appId)
        {
            using (var db = new MainDbContext())
            {
                var invitee = db.Invitees.Create();
                invitee.Email = Email;
                invitee.Presence = Presence;
                db.Invitees.Add(invitee);
                db.SaveChanges();
            }

        }
    }
}

“邀请者”表还包括一个字段,其中包含将每条记录链接到约会的约会ID。 当我检查调试器中的innerException时,我发现由于NullReference错误而导致错误。 此错误是由名为Appointment_Id的字段引起的,该字段会自动添加到内部db.Invitees SQL中。

当我取消注册Invitee类中的Appointment_Id时,会出现'Unknown column Appoinment_Id1'错误。

如何在Appointment.Id中获取invitee.appointment_Id的值?

1 个答案:

答案 0 :(得分:0)

  

当我取消注释Invitee类中的Appointment_Id时,会出现“未知列Appoinment_Id1”错误。

从这一行开始,我会说你的模型有问题。从你的模特中看......我会说你错过了

public Appointment Appointment { get; set; }

在你的Invitee课程中。

您可以阅读以下主题:http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

更新1 我还建议您使用相同的DbContext来保存两个实体。或者至少考虑使用TransactionScope来确保在一个事务中保存所有这些。 也许想想你是否应该在for循环中调用SaveChanges()。