2012-01-15 93 views
0

我剛剛開始使用EF CF(從PHP/MySQL的土地來),我希望有人能幫助我理解我在做什麼錯在這裏。EF代碼第一次實體關係問題

使用那裏的許多偉大的教程,我創建了我的模型類與關係和DB正在創建和更新確定。但是現在我遇到了Cascade Delete的問題。

從我的理解,當一個實體可以通過級聯刪除刪除比的方式從而產生孤立記錄更多,這是造成 - 是這樣嗎?

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 

誰能幫助我明白我做錯了,我怎麼可以解決它嗎?任何其他建議也歡迎!

在此先感謝。

回答

0

其實你沒有做錯任何事,但SQL Server不喜歡處理多個級聯刪除路徑。實際上有兩個刪除路徑從ResidencePool

  1. 的直接關係,並
  2. 通過Residence - >Job - >JobService - >Pool

模型中的所有外鍵列是不可爲空。因此EF試圖通過級聯刪除來創建FK。

爲了避免這個問題,使其中一個FK可以爲空,並通過替代機制(如存儲過程,觸發器,通過代碼)來處理子實體的刪除。

+0

謝謝Eranga。這就說得通了! – nugget 2012-01-17 13:32:05