我正在尝试将三个数据库表中的一些数据放入一个视图中。因此,我使用一个类,其中包含另外两个带有列表的类。
这两个包含的类中的一个运行良好,但另一个在控制器ActionResult中导致InvalidOperationException。
这是我到目前为止产生的结果:
类
namespace ArrangeAppointments.Models
{
public class Appointment : IAppointment
{
#region Implementation of IAppointments
public int UserId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Duration { get; set; }
public virtual List<Invitee> Invitees { get; set; }
public virtual List<Occasion> Occasions { get; set; }
#endregion
public int AppointmentId { get; set; }
namespace ArrangeAppointments.Models
{
public class Invitee : IInvitee
{
public int Id { get; set; }
public int AppointmentId { get; set; }
public string Email { get; set; }
public string Presence { get; set; }
public virtual Appointment Appointment { get; set; }
namespace ArrangeAppointments.Models
{
public class Occasion : IOccasion
{
public int Id { get; set; }
public int AppointmentId { get; set; }
public DateTime ocDate { get; set; }
public DateTime ocTime { get; set; }
public string Location { get; set; }
public virtual Appointment Appointment { get; set; }ActionResult
[HttpGet]
public ActionResult NewApp(int Id)
{
if (Id == 0)
{
return View();
} else {
try
{
using (var db = new MainDbContext())
{
Appointment appointment = db.Appointments.Find(Id);
if (appointment == null)
{
return HttpNotFound();
}
else
{
return View("NewApp", appointment);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.InnerException);
return View();
}
}
}ActionResult中的对象'appointment‘填充了所有必需的数据,但用于场合的列表除外。这会导致异常。但是,此异常不会触发catch事件。
是什么导致了这种异常?
DbContext类
namespace ArrangeAppointments
{
public class MainDbContext : DbContext
{
public MainDbContext() : base("name=DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Appointment> Appointments { get; set; }
public DbSet<Invitee> Invitees { get; set; }
public DbSet<Occasion> Occasions { get; set; }
}
}发布于 2016-06-21 15:34:39
您需要包括列表
Appointment appointment = db.Appointments.include("Occasions").include("Invitees").SingleOrDefault(a => a.AppointmentId == Id);https://stackoverflow.com/questions/37937825
复制相似问题