【发布时间】:2011-05-11 22:26:41
【问题描述】:
我无法从我的数据库中删除一个值对象,因为它与不同的实体有两个关系,它只是将一个外键设置为 null,这会引发。这是我的域名:
public class Product : IEntity
{
public int Id { get; set; }
public string ModelNumber { get; set; }
public ICollection<ProductQuantity> ProductQuantities { get; set; }
}
public class Project: IEntity
{
public int Id { get; set; }
public string SpecConnect { get; set; }
public virtual ICollection<ProductQuantity> ProductQuantitys { get; set; }
}
public class ProductQuantity
{
public int Id { get; set; }
public virtual Product Product { get; set; }
public virtual Project Project { get; set; }
}
这是我从项目中删除 ProductQuantity 的代码:
var product = _productRepository.GetById(id);
var project = _projectRepository.GetById(projectId);
var productQuantity = project.ProductQuantitys.SingleOrDefault(x => x.Product == product);
project.ProductQuantitys.Remove(productQuantity);
感谢您的任何想法!
【问题讨论】:
标签: ef-code-first entity-framework-4.1