2012-01-15 3 views
0

저는 EF CF (PHP/MySQL의 출처)에서 시작하고 있습니다. 누군가 내가 여기서 잘못하고있는 것을 이해하도록 도와 줄 수 있기를 바랍니다.EF 코드 첫 번째 엔티티 관계 문제

많은 위대한 튜토리얼을 사용하여 관계형 모델 클래스를 만들었고 DB가 생성되고 업데이트되었습니다. 하지만 이제 캐스케이드 삭제에 문제가 있습니다.

내가 알기로 이것은 엔티티가 계단식 삭제를 통해 삭제되어 고아 레코드가 생성 될 때 발생합니다. 바로 그 것입니까?

public class jbsEntity 
{ 
    [Timestamp] 
    public Byte[] TimeStamp { get; set; } 

    //public User CreatedBy { get; set; } 
    //public User UpdatedBy { get; set; } 
} 

public class Customer : jbsEntity 
{ 
    public int Id { get; set; } 
    public string Salutation { get; set; } 

    [Required, StringLength(128)] 
    public string FirstName { get; set; } 

    [Required, StringLength(128)] 
    public string LastName { get; set; } 

    public string BillingName { get; set; } 

    [Required, StringLength(128)] 
    public string Address { get; set; } 

    [Required, StringLength(128)] 
    public string Suburb { get; set; } 

    [Required, StringLength(4)] 
    public string Postcode { get; set; } 

    public string BillingAddress { get; set; } 
    public string BillingSuburb { get; set; } 
    public string BillingPostcode { get; set; } 

    public virtual List<Contact> Contacts { get; set; } 

    public virtual List<Residence> Residences { get; set; } 

} 

public class Residence : jbsEntity 
{ 
    public int Id { get; set; } 

    [Required, StringLength(128)] 
    public string Name { get; set; } 

    [Required, StringLength(128)] 
    public string Address { get; set; } 

    [Required, StringLength(128)] 
    public string Suburb { get; set; } 

    [Required, StringLength(4)] 
    public string Postcode { get; set; } 

    public bool Active { get; set; } 
    public string AccessInstructions { get; set; } 
    public string ServiceFrequency { get; set; } 
    public string RegularServiceCost { get; set; } 

    [Required] 
    public int CustomerId { get; set; } 
    public virtual Customer Customer { get; set; } 

    public virtual List<Contact> Contacts { get; set; } 

    public virtual List<Pool> Pools { get; set; } 

    public virtual List<Job> Jobs { get; set; } 

    public virtual List<Image> Images { get; set; } 

} 

public class Pool : jbsEntity 
{ 
    public int Id { get; set; } 

    [Required, StringLength(128)] 
    public string Name { get; set; } 

    [Required, StringLength(128)] 
    public string Size { get; set; } 
    public string AccessInstructions { get; set; } 
    public string SurfcaceType { get; set; } 
    public bool Indoor { get; set; } 
    public decimal TargetPH { get; set; } 
    public decimal TargetIron { get; set; } 
    public decimal TargetCopper { get; set; } 
    public decimal TargetCalcium { get; set; } 
    public decimal TargetChlorine { get; set; } 
    public decimal TargetAlkaline { get; set; } 
    public decimal TargetStabiliser { get; set; } 

    [Required] 
    public int ResidenceId { get; set; } 
    public virtual Residence Residence { get; set; } 

    public virtual List<JobService> JobServices { get; set; } 

    public virtual List<Image> Images { get; set; } 

} 

public class Equipment : jbsEntity 
{ 
    public int Id { get; set; } 

    [Required, StringLength(128)] 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public DateTime Installed { get; set; } 
    public DateTime WarrantyExpiration { get; set; } 
    public string Status { get; set; } 

    [Required] 
    public int PoolId { get; set; } 
    public virtual Pool Pool { get; set; } 

    public virtual List<Image> Images { get; set; } 

} 

public class Contact : jbsEntity 
{ 
    public int Id { get; set; } 
    public string Salutation { get; set; } 

    [Required, StringLength(128)] 
    public string FirstName { get; set; } 

    [Required, StringLength(128)] 
    public string LastName { get; set; } 
    public string Email { get; set; } 
    public string Mobile { get; set; } 
    public string Phone { get; set; } 

} 

public class Image : jbsEntity 
{ 
    public int Id { get; set; } 
    public string FileName { get; set; } 
    public string FilePath { get; set; } 
    public string MimeType { get; set; } 
} 

public class Job : jbsEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string InvoiceNumber { get; set; } 
    public string Comments { get; set; } 
    public string CompletionNotes { get; set; } 
    public string DpscActionRequired { get; set; } 
    public string WorkPerformed { get; set; } 
    public string NextCallInstruction { get; set; } 
    public bool ServiceComplete { get; set; } 
    public DateTime DueDate { get; set; } 
    public DateTime ScheduledDate { get; set; } 
    public DateTime CompletionDate { get; set; } 

    [Required] 
    public int ResidenceId { get; set; } 
    public virtual Residence Residence { get; set; } 

    public virtual List<JobService> JobServices { get; set; } 

    public virtual List<Image> Images { get; set; } 

} 

public class JobService : jbsEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool SkimSurface { get; set; } 
    public bool BrushWalls { get; set; } 
    public bool Vacuum { get; set; } 
    public bool BackwashRinse { get; set; } 
    public bool EmptyBaskets { get; set; } 
    public bool CleanElectrodes { get; set; } 
    public bool BalanceWater { get; set; } 
    public bool AddedAcid { get; set; } 
    public bool AddedClarifier { get; set; } 
    public bool SwimClearOnsite { get; set; } 
    public decimal PH { get; set; } 
    public decimal Iron { get; set; } 
    public decimal Copper { get; set; } 
    public decimal Calcium { get; set; } 
    public decimal Chlorine { get; set; } 
    public decimal Alkaline { get; set; } 
    public decimal Stabiliser { get; set; } 

    [Required] 
    public int JobId { get; set; } 
    public virtual Job Job { get; set; } 

    [Required] 
    public int PoolId { get; set; } 
    public virtual Pool Pool { get; set; } 

    public virtual List<Image> Images { get; set; } 

} 


public class User : jbsEntity 
{ 
    public int Id { get; set; } 
    [Required] 
    public string UserName { get; set; } 
    [Required] 
    public string FirstName { get; set; } 
    [Required] 
    public string LastName { get; set; } 
    [Required] 
    public string Password { get; set; } 
    [Required, Compare("Password")] 
    public string ComparePassword { get; set; } 
} 

나는이 오류 얻을 :

{"Introducing FOREIGN KEY constraint 'Pool_Residence' on table 'Pools' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors."} 

내가 문제를 일으키는 관계가 생각 :

Customer -> Residences -> Pools 
Job -> JobServices 
Residence -> Jobs 
Pools -> JobServices 

사람이 나를 내가 잘못 이해하는 데 도움 수를 어떻게 I 그걸 해결할 수 있니? 다른 제안도 환영합니다!

미리 감사드립니다.

답변

0

실제로 잘못된 것은 아니지만 SQL Server는 다중 계단식 삭제 경로를 처리하지 않습니다. 실제로 Residence에서 Pool까지 삭제 경로가 두 개 있습니다.

  1. 다이렉트 관계 및 Residence 통해
  2. ->Job ->JobService - 모델에서>Pool

는 외래 키 컬럼의 모든 비 널 (NULL)입니다. 따라서 EF는 계단식 삭제를 사용하여 FK를 만들려고합니다.

이 문제를 피하려면 FK 중 하나를 Null 가능하게 만들고 저장 프로 시저, 트리거, 통과 코드와 같은 대체 메커니즘을 통해 하위 엔터티 삭제를 처리하십시오.

+0

고맙습니다. Eranga. 그건 의미가 있습니다! – nugget