[英]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.