在Entity Framework Code First中删除自引用实体

时间:2013-05-31 18:29:43

标签: wpf entity-framework mvvm-light

我在wpf中为我的EF定义了以下类,用于mvvm模式:

public class ProductGroup : ViewModelBase
{

    public long ID { get; set; }

    private string _Title;
    public string Title
    {
        get { return _Title; }
        set
        {
            if (_Title != value)
            {
                _Title = value;
                this.RaisePropertyChanged("Title");
            }
        }
    }

    private byte[] _Icon;
    public byte[] Icon
    {
        get { return _Icon; }
        set
        {
            if (_Icon != value)
            {
                _Icon = value;
                this.RaisePropertyChanged("Icon");
            }
        }
    }

    private ProductGroup _Parent;
    public ProductGroup Parent
    {
        get { return _Parent; }
        set
        {
            if (_Parent != value)
            {
                _Parent = value;
                this.RaisePropertyChanged("Parent");
            }
        }
    }

    private ObservableCollection<ProductGroup> _Childs;
    public ObservableCollection<ProductGroup> Childs
    {
        get { return _Childs; }
        set
        {
            if (_Childs != value)
            {
                _Childs = value;
                this.RaisePropertyChanged("Childs");
            }
        }
    }

    private ObservableCollection<Product> _Products;
    public ObservableCollection<Product> Products
    {
        get { return _Products; }
        set
        {
            if (_Products != value)
            {
                _Products = value;
                this.RaisePropertyChanged("Products");
            }
        }
    }

}

public class Product : ViewModelBase
{
    public long ID { get; set; }

    private ProductGroup _Gorup;
    public ProductGroup Gorup
    {
        get { return _Gorup; }
        set
        {
            if (_Gorup != value)
            {
                _Gorup = value;
                this.RaisePropertyChanged("Gorup");
            }
        }
    }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            if (_Name != value)
            {
                _Name = value;
                this.RaisePropertyChanged("Name");
            }
        }
    }

}
  • 我的定义是否正确?
  • 当我想从ProductGroups中删除项目时,会发生以下错误:

保存未公开其关系的外键属性的实体时发生错误。 EntityEntries属性将返回null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。有关详细信息,请参阅InnerException。

注意:

  • 目标ProductGroup是另一个ProductGroup的子项

  • 目标ProductGroup没有子级和产品

0 个答案:

没有答案