ASP.net MVC 4尝试删除记录时出错

时间:2013-03-06 05:48:32

标签: c# database asp.net-mvc-4

我正在尝试删除客户记录。客户端包含一个地址,我想我的删除顺序与他们的关系有问题。基本上我想删除一个客户端,如果他们有一个地址,要删除它。 这是我得到的完整异常错误消息:

  

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");
    }

我的订单或删除有问题吗?或者我只是不做正确的事情?我只是希望能够删除有或没有地址的客户。

1 个答案:

答案 0 :(得分:0)

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