entity framework – 使用外键删除对象,保留父对象
我有以下型号:
public class Company { //Primary key public string ID { get; set; } //Foreign key public int? LogotypeID { get; set; } }
和
public class Logotype { //Primary key [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int? ID { get; set; } //Foreign key public string CompanyID { get; set; } }
如何在不删除公司行的情况下从Company表中删除标识?
使用:
http://msdn.microsoft.com/en-us/library/system.data.entity.dbset.remove(v=vs.113).aspx DbSet.Remove(Logotype)引发以下exception:
{"The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Companies_dbo.Logotypes_LogotypeID\". The conflict occurred in database \"ShipReg\", table \"dbo.Companies\", column 'LogotypeID'.\r\nThe statement has been terminated."}
有任何想法吗?
布朗,蒂姆
在公司中添加虚拟财产
public class Company { //Primary key public string ID { get; set; } //Foreign key public int? LogotypeID { get; set; } public virtual Logotype Logotype {get;set;} }
然后
dbContext.Entry(company).State=EntityState.Modified; dbContext.Entry(company.Logotype).State=EntityState.Deleted;