2

我正在尝试删除客户记录。客户包含一个地址,我想我在涉及他们的关系时遇到了删除顺序的问题。基本上我想删除一个客户,如果他们有一个地址,就把它连同它一起删除。这是我收到的完整异常错误消息:

用户代码未处理 DbUpdateException

保存不为其关系公开外键属性的实体时发生错误。EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。有关详细信息,请参阅 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");
    }

我的订单或删除有问题吗?或者我只是没有做对吗?我只想能够删除有或没有地址的客户。

4

1 回答 1

0

从地址中删除对客户端的引用。只需保留客户对地址的引用即可。

于 2013-03-25T08:34:30.340 回答