2014-04-04 103 views
0

我有以下型號:實體框架 - 與外鍵刪除對象,並保留父

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

如何從公司表中刪除標識,沒有刪除公司行?

使用:
http://msdn.microsoft.com/en-us/library/system.data.entity.dbset.remove(v=vs.113).aspx DbSet.Remove(打印商標)thows以下異常:

{"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."} 

任何想法?

BR, 添

+0

是公司ID是外鍵? –

+0

您使用的是數據遷移嗎?如果是,您是否將級聯刪除設置爲true? – Marc

+0

@Rudresh。是的,CompanyId是外鍵。 –

回答

2

在打印商標表中的公司加入虛擬財產一樣

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; 
+0

謝謝,它工作! –

+0

歡迎您:) –