实体框架 - 删除带有外键的对象,保留父对象

Tim*_*son 1 c# entity-framework ef-code-first

我有以下型号:

public class Company
{
    //Primary key
    public string ID { get; set; } 

    //Foreign key
    public int? LogotypeID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

public class Logotype
{
    //Primary key
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int? ID { get; set; }

    //Foreign key
    public string CompanyID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如何从公司表中删除标识而不删除公司行?

使用:
http://msdn.microsoft.com/en-us/library/system.data.entity.dbset.remove (v=vs.113).aspx DbSet.Remove(Logotype) 会引发以下异常:

{"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."}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

兄弟,蒂姆

Rud*_*ppa 5

在公司中添加虚拟财产,例如

public class Company
{
    //Primary key
    public string ID { get; set; } 

    //Foreign key
    public int? LogotypeID { get; set; }

    public virtual Logotype Logotype {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

进而

dbContext.Entry<Company>(company).State=EntityState.Modified;
dbContext.Entry<Logotype>(company.Logotype).State=EntityState.Deleted;
Run Code Online (Sandbox Code Playgroud)