2013-08-30 1 views
1

Entity Framework를 처음 사용하고 ASP.Net MVC 4 응용 프로그램에서이 작업을 수행하는 방법을 이해하려고합니다.Entity Framework에서 도메인 클래스에 여러 개체를 데이터베이스에 추가하지 않음

고객 개체 (고객 정보)와 약속의 DateTime이 포함 된 Appointment 개체가 있습니다. 고객 개체를 올바르게 저장하는 방법을 파악할 수 없습니다.

내가 지금하고있는 일을보고 나는 고객 ID를 저장해야한다고 생각하지만 나중에 고객 정보를 검색하는 방법을 모릅니다 (모델을 사용합니까? 다른 도메인 클래스 "AppointmentDetails")? 나는 서비스 계층 오브젝트를 사용할 수 있습니까?)

public class Appointment 
    { 
     public int Id { get; set; } 
     public Customer Customer { get; set; } 
     public DateTime AppointmentDateTime { get; set; } 

     public Appointment() 
     { 

     } 

     public Appointment(Customer customer, DateTime appointmentDateTime) 
     { 
      Customer = customer; 
      AppointmentDateTime = appointmentDateTime; 
     } 
    } 

Customer.cs

public class Customer 
    { 
     public int Id { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Address { get; set; } 
     public string City { get; set; } 
     public string Province { get; set; } 
     public string Phone { get; set; } 
     public string Email { get; set; } 

     public Customer() 
     { 

     } 

     public Customer(string firstName, string lastName, string address, string city, string province, string phone, string email) 
     { 
      FirstName = firstName; 
      LastName = lastName; 
      Address = address; 
      City = city; 
      Province = province; 
      Phone = phone; 
      Email = email; 
     } 
    } 
+0

더 많은 코드를 게시하십시오. 여기 고객은 수업입니다. 그것을 정의해야합니다. – Kalyan

+0

어떤 코드를보고 싶습니까? AppointmentService, 저장소 (DbContext), 컨트롤러, 모델이 있습니다. 그것 모두를 게시하는 것이 많이 있습니까? 그것이 필요한 경우 할 수 있습니다. – devfunkd

+0

Customer Class를 정의 했습니까? – Kalyan

답변

0

지금까지, 난 항상 DB 첫 번째 방법은 다음 엔티티 프레임 워크 사용했지만, 나는 수업이 뭔가를 저장한다고 생각 like :

귀하의 예약 클래스에 당신이 가진 것 동안 고객 클래스의
public virtual ICollection<Appointment> Appointment{ get; set; } // which will be used to access your Customer's appointments. 

:

public int CustomerId { get; set; } 
public virtual Customer Customer { get; set; } 

어떻게 당신은 그 클래스를 창조 하셨 는가? 모델 디자이너를 사용하면 클래스를 연결하는 데 필요한 속성을 생성합니다.

+1

먼저 모델을 만들고 코드를 사용하여 테이블을 생성했습니다. 나는 내 흐름이 어떤지에 대해 궁금해하고 있었고 아마도 약속을 가지고 고객 ID 만 저장하고 고객 정보를 검색하기 위해 내 서비스를 사용한다고 생각했습니다 ... 문제는 내 약속이있는 경우 고객 정보를 어디에 저장할 것인지를 파악할 수 없었습니다 클래스에는 고객 ID 속성 만있었습니다. 가상 속성을 사용하지 마십시오. 감사! – devfunkd

관련 문제