且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架 - 使用外键删除对象,保留父项

更新时间:2022-03-24 23:06:14

添加公司中的虚拟财产如

add a virtual property in company like

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>(company).State=EntityState.Modified;
dbContext.Entry<Logotype>(company.Logotype).State=EntityState.Deleted;