2013-03-06 3 views
1

클라이언트 레코드를 삭제하려고합니다. 클라이언트는 주소를 포함하고 있으며 나는 그들의 관계에 관해서 삭제 순서에 문제가 있다고 생각합니다. 기본적으로 클라이언트를 삭제하고 주소가있는 경우이를 삭제하려고합니다. 이 내가 얻을 전체 예외 오류 메시지입니다 :ASP.net MVC 4 레코드를 삭제하려고 할 때 오류가 발생했습니다.

DbUpdateException 그들의 관계에 대한 외래 키 속성을 노출하지 않는 개체를 저장하는 동안 오류가 발생 사용자 코드

에 의해 처리되지 않은했다. 단일 엔터티가 예외의 소스로 식별 될 수 없으므로 EntityEntries 속성은 을 반환합니다. 엔터티 형식에 외래 키 속성을 노출하여 저장 중에 예외 처리를 더 쉽게 수행 할 수 있습니다 ( ). 자세한 내용은 InnerException을 참조하십시오.

모델

public class Address 
{ 
    [Required] 
    public int Id { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Street Address")] 
    public string StreetAddress { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Postal Code")] 
    public string PostalCode { get; set; } 

    [DataType(DataType.Text)] 
    public string City {get; set; } 

    [DataType(DataType.Text)] 
    public string Province {get; set;} 

    public virtual Clients client { get; set; } 

} 
public class Clients 
{ 
    [Required] 
    public long Id { get; set; } 

    [Required] 
    [DataType(DataType.Text)] 
    [Display(Name = "First Name")] 
    public string FirstName { get; set; } 

    [Required] 
    [DataType(DataType.Text)] 
    [Display(Name = "Last Name")] 
    public string LastName { get; set; } 

    [Required] 
    [DataType(DataType.PhoneNumber)] 
    [Display(Name = "Phone ")] 
    public string PhoneNumber { get; set; } 

    public virtual Address Address {get; set;} 

    [Display(Name = "Email List")] 
    public Boolean EmailList { get; set; } 

    [DataType(DataType.EmailAddress)] 
    [Display(Name = "E-mail")] 
    public string Email { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Hair Type")] 
    public string HairType { get; set; }   

    [DataType(DataType.MultilineText)] 
    public string Description { get; set; } 
} 

상황에 맞는 클래스

public class VolumeV2Context : DbContext 
{ 
    public DbSet<GiftCard> GiftCards { get; set; } 
    public DbSet<Clients> Clients { get; set; } 
    public DbSet<Address> Address { get; set; } 
    public DbSet<Inventory> Inventories { get; set; } 


    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Clients>() 
      .HasOptional(j => j.Address) 
      .WithOptionalDependent() 
      .WillCascadeOnDelete(true); 

     /* modelBuilder.Entity<Address>() 
      .HasRequired(j => j.client) 
      .WithRequiredDependent() 
      .WillCascadeOnDelete(true) ;       
     */ 


     base.OnModelCreating(modelBuilder); 
    } 
} 

클라이언트 컨트롤러 삭제 방법

[HttpPost, ActionName("Delete")] 
    public ActionResult DeleteConfirmed(long id) 
    { 
     //find the client 
     Clients clients = db.Clients.Find(id); 

     //find the address 
     Address address = db.Address.Find(clients.Address.Id); 

     // set the reference to null? 
     address.client = null;      

     //remove the address foreign key? 
     clients.Address = null; 

     //Apply to db? 
     db.Entry(address).CurrentValues.SetValues(address); 
     db.Entry(clients).CurrentValues.SetValues(clients); 

     db.Address.Remove(address); 

     //remove the client 
     db.Clients.Remove(clients); 
     //exception error happens here 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

주문 또는 삭제에 문제가 있습니까? 또는 나는 다만 무언가를 정확하게하고 있지 않는가? 주소가 있거나없는 클라이언트를 제거 할 수 있기를 바랍니다.

+1

plz 게시 작업의 내부 예외가 게시됩니다. –

+0

죄송합니다. 나는 지금 – Fpanico

+0

예외를 얻을 때 내부 예외 메시지를 복사하고 여기에 게시 –

답변

0

주소에서 클라이언트로 참조를 제거하십시오. 고객으로부터 주소로의 참조 만하면됩니다.

관련 문제