简体   繁体   English

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

[英]ASP.net MVC 4 Error when trying to delete a record

I'm trying to delete a client record.我正在尝试删除客户记录。 The client contains an address and I guess I'm having issues with the order of deletion when it comes to their relationship.客户端包含一个地址,我想我在删除它们的关系时遇到了问题。 Basically I want to delete a client and if they have an address, to delete that along with it.基本上我想删除一个客户端,如果他们有一个地址,将它连同它一起删除。 This is the complete exception error message I get:这是我收到的完整异常错误消息:

DbUpdateException was unhandled by user code用户代码未处理 DbUpdateException

An error occurred while saving entities that do not expose foreign key properties for their relationships.保存未公开其关系的外键属性的实体时发生错误。 The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。 Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。 See the InnerException for details.有关详细信息,请参阅 InnerException。

Models楷模

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

Context Class上下文类

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

Client Controller Delete Method客户端控制器删除方法

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

Is there something wrong with my order or deletion ?我的订单或删除有什么问题吗? or am I just not doing something right?或者我只是做错了什么? I just want to have the ability to remove clients with or without an address.我只想能够删除有地址或没有地址的客户端。

Remove the reference from an address to a client.删除从地址到客户端的引用。 Just keep your reference from a client to an address, that will do.只需保留您从客户到地址的参考,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 5.0 Web API 在尝试使用 HttpDelete 删除记录时抛出错误 405 - ASP.NET Core 5.0 Web API throws error 405 when trying to delete record using HttpDelete 尝试在asp.net mvc中注册用户时出错 - Error When trying to Register a User in asp.net mvc 尝试在ASP.Net MVC网站中启用CORS时出错 - Error when trying to enable CORS in ASP.Net MVC website ASP.Net MVC删除具有FK约束的记录 - ASP.Net MVC Delete record with a FK Constraint 在ASP.Net MVC中删除记录的安全方法 - Secure way to Delete a record in ASP.Net MVC Asp.NET MVC:尝试调用删除控制器方法时出错 - Asp.NET MVC : Error while trying to call delete controller method 尝试在ASP.NET MVC中保存时,计划更新/删除/添加操作不会保留 - Schedule update/delete/add operations do not persist when trying to save in ASP.NET MVC 删除客户记录时删除ASP.NET用户帐户? - Delete ASP.NET user account when customer record is deleted? 当用户尝试删除具有外键约束的对象时,在ASP.NET MVC中显示错误页面? - Display an error page in ASP.NET MVC when a user attempts to delete an object with Foreign Key constraints? C#ASP.Net-尝试从表中删除数据时出错 - C# ASP.Net - Error when trying to delete data from table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM