0

我的数据库中有 2 个表:

[Table]
public class Names: INotifyPropertyChanged, INotifyPropertyChanging
{        
    public Names()
    {
        this._namesCar = new EntitySet<Coord>(this.OnCarAdded, this.OnCarRemoved);
    }

    private int _id;
    private string _name;
    private EntitySet<Cars> _namesCar;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync= AutoSync.OnInsert)]
    public int Id
    {
        get { return _id; }
        set
        {
            if (_id != value)
            {
                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }
    }

    [Column]
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                NotifyPropertyChanging("Name");
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    [Association(Storage = "_namesCar", ThisKey = "Id", OtherKey = "NamesId")]
    public EntitySet<Cars> NamesCar
    {
        get { return _namesCar; }
        set
        {
            if (_namesCar != value)
            {
                NotifyPropertyChanging("NamesCar");
                _namesCar = value;
                NotifyPropertyChanged("NamesCar");
            }
        }
    }

    private void OnCarAdded(Cars car)
    {
        car.Name= this;
    }

    private void OnCarRemoved(Cars car)
    {
        car.Name= null;
    }       

[Table]
public class Cars: INotifyPropertyChanged, INotifyPropertyChanging
{

    private int _id;
    private int _nameid;
    private string _cars;        
    private EntityRef<Names> _carNames = new EntityRef<Names>();

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int Id
    {
        get { return _id; }
        set
        {
            if (_id != value)
            {
                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }
    }

    [Column(DbType = "Int")]
    public int NameId
    {
        get { return _nameid; }
        set
        {
            if (_nameid != value)
            {
                NotifyPropertyChanging("NameId");
                _nameid = value;
                NotifyPropertyChanged("NameId");
            }
        }
    }

    [Association(Storage = "_carNames", ThisKey = "NameId", OtherKey = "Id", IsForeignKey = true)]
    public Names Names
    {
        get { return this._carNames.Entity; }
        set
        {
            NotifyPropertyChanging("Names");
            this._carNames.Entity = value;
            if (value != null)
            {                    
                this._nameid = value.Id;
            }
            NotifyPropertyChanged("Names");
        }
    }       

它们属于相同的数据上下文:

public class CarsDataContext : DataContext
{
    // Pass the connection string to the base class.
    public CarsDataContext(string connectionString)
        : base(connectionString)
    { }

    // Specify a table for the items.
    public Table<Names> RecNames;
    public Table<Cars> RecCars;

}

我可以添加一个新名称,然后添加与该名称关联的新车。我也可以从一个名字中删除汽车。但是,当我尝试删除名称时,我遇到了异常。我相信这是因为有与该名称相关的汽车。删除包含与其关联的汽车的名称的最佳方法是什么?我的意图是先删除所有汽车,然后再删除名称。

谢谢!

4

1 回答 1

0

尝试使用DeleteRule属性的Association属性,特别是使用备注中指示的“CASCADE”:

如果设置为 null,则不添加删除行为。例如,“CASCADE”会将“ON DELETE CASCADE”添加到外键关系中。

于 2013-01-25T21:26:31.417 回答