EF code首先外键的 [英] EF Code First Foreign Key's

查看:131
本文介绍了EF code首先外键的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的EF code库首先尝试在预约调度应用工作的工作。

I am working with the EF Code First library trying to work on an appointment scheduling app.

该模型的我都将是一个客户端,约会,AppointmentType ...

The model's I have are going to be a Client, Appointment, and AppointmentType...

基本上每个客户端可以有一组预约的,并且每个预约可以具有单个AppointmentType ...

Basically each Client can have a set of Appointments, and each Appointment can have a single AppointmentType...

在code是如下:

public class Client
 {
      [ScaffoldColumn(false)]
      public int ClientID { get; set; }

      [Required]
      public string FirstName { get; set; }

      [Required]
      public string LastName { get; set; }

      [EmailAddress]
      [Required]
      public string Email { get; set; }

      [DataType("DateTime")]
      public DateTime Birthday { get; set; }

      [Required]
      public string CellPhone { get; set; }

      public string HomePhone { get; set; }
      public string Notes { get; set; }

      public virtual ICollection<Appointment> Appointments{ get; set; }

      public string Name { 
           get{
                return FirstName + " " + LastName;
           }
      }
public class Appointment
 {
      [ScaffoldColumn(false)]
      public int AppointmentID { get; set; }

      [ScaffoldColumn(false)]
      public int ClientID { get; set; }

      [ScaffoldColumn(false)]
      public int AppointmentTypeID { get; set; }

      [Required]
      public DateTime AppointmentDate { get; set; }
      public string Notes { get; set; }

      public virtual AppointmentType AppointmentType { get; set; }
      public virtual Client Client { get; set; }
 }
     public class AppointmentType
 {
      [ScaffoldColumn(false)]
      public int AppointmentTypeID { get; set; }

      [Required]
      public string Name { get; set; }
      public string Description { get; set; }

      public virtual Appointment Appointment { get; set; }
 }

一切都工作得很好,当我创建约会类型,一个客户端,但是当我创建约会我得到以下错误...

Everything works well when I create an appointment type, and a client, but when I create an appointment I get the following error...


  • 的InnerException {INSERT语句冲突与外键约束\\Appointment_Client \\。该冲突发生于数据库\\Salon.Models.SalonContext \\,表\\dbo.Clients \\,列客户端ID。 \\ r \\ n此语句已终止。} {System.Exception的} System.Data.SqlClient.SqlException

如果需要更多的详细信息,请让我知道......我只是想弄清楚,如果我错过了什么设置

If more details are needed, please let me know...I am just trying to figure out if I am missing anything in the setup.

这是当我在调试后创建的约会会发生什么......所有的ID是为0,这是正确的,但应该在其他领域不能为空?或者有什么关系......只是不是很熟悉的事情应该怎么看这是我第一次EF code第一个项目...

This is what happens when I debug on the post to create the Appointment...All the ID's are as 0 which is correct, but should the other fields not be null?? Or does it matter...Just not very familiar with how things should look this being my first EF Code First project...

推荐答案

根据您的设​​置,人们AppointmentType只能有一个约会。这是一个一对一的映射。在这种情况下,你最好移动AppointmentType到约会实体。否则,我相信更符合逻辑,一个AppoitmentType可以有很多,但任命一个约会只能有一个AppointmentType。因此,你应该有你的AppointmentType实体内部的一个虚拟的ICollection。

According to your setup, one AppointmentType can only have one Appointment. This is a one-to-one mapping. In this case, you better move the AppointmentType into the Appointment entity. Otherwise, what I believe is more logical, an AppoitmentType can have many Appointments but one Appointment can have only one AppointmentType. Accordingly, you should have a virtual ICollection inside your AppointmentType entity.

 public class AppointmentType
 {
      [ScaffoldColumn(false)]
      public int AppointmentTypeID { get; set; }

      [Required]
      public string Name { get; set; }
      public string Description { get; set; }

      public virtual ICollection<Appointment> Appointments { get; set; }
 }

我不知道这是什么造成的问题,但它可能是。有时候,映射故障导致引发一些奇怪的例外。给它一个尝试,让我知道,如果你的问题得到解决。

I am not sure this is what's causing the problem but it could be. Sometimes mapping faults cause some weird exceptions to be thrown. Give it a try and let me know if your problem gets resolved.

这篇关于EF code首先外键的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆