1

我的 linq 数据库存在一对多关系的问题。以下是课程:

    private EntityRef<Account> _account;

    [Association(Storage = "_account", ThisKey = "IDMovement", OtherKey = "IDAccount", IsForeignKey = true)]
    public Account Account
    {
        get { return this._account.Entity; }
        set
        {
            Account previousValue = this._account.Entity;
            if (((previousValue != value) || (this._account.HasLoadedOrAssignedValue == false)))
            {
                NotifyPropertyChanging("Account");
                if ((previousValue != null))
                {
                    this._account.Entity = null;
                    previousValue.Movements.Remove(this);
                }
                this._account.Entity = value;
                if ((value != null))
                {
                    value.Movements.Add(this);
                }
                NotifyPropertyChanged("Account");
            }
        }
    }

另一方面:

private EntitySet<Movement> _movements;

    [Association(Storage = "_movements", OtherKey = "IDMovement", ThisKey = "IDAccount")]
    public EntitySet<Movement> Movements
    {
        get { return this._movements; }
        set { this._movements.Assign(value); }
    }

    private void OnMovementAdded(Movement movement)
    {
        NotifyPropertyChanging("Movement");
        movement.Account = this;
        NotifyPropertyChanged("Movement");
    }

    private void OnMovementRemoved(Movement movement)
    {
        NotifyPropertyChanging("Movement");
        movement.Account = null;
        NotifyPropertyChanged("Movement");
    }

我的问题是,当我必须在数据库中插入新动作时,我不知道如何为“EntityRef _account”变量分配数据库中已有的值。

例如:我必须为帐户“general”插入一个动作,这个帐户当然已经在我的数据库中......我怎么能对我的动作说它在“movement.Account”中的帐户“general”它与我的数据库中包含的完全相同吗?

我尝试了类似的方法,但它不起作用,因为就像我创建了一个新帐户一样:

Movement movement = new Movement();
            movement.Category = (from f in context.Categories
                         where f.Name == this.Categories.ElementAt(this.ChoosenCategory).Name
                         select f).FirstOrDefault();
            movement.Account = (from f in context.Accounts
                     where f.Name == ChoosenAccount.Name
                     select f).FirstOrDefault();

            context.Movements.InsertOnSubmit(movement);
            context.SubmitChanges();
4

1 回答 1

0

您需要为 EntityRef 调用Load()方法,例如if (!order.CustomersReference.IsLoaded) order.CustomersReference.Load();

于 2012-10-01T15:09:53.673 回答