开发者

Bug in generated LINQ to SQL class?

开发者 https://www.devze.com 2023-04-06 14:07 出处:网络
I have two tables: Budgets and Consultants. There is a 1 to 0..1 relation between Budgets and Consultants: a Budget has a Consultant and a Consultant can have a Budget.

I have two tables: Budgets and Consultants. There is a 1 to 0..1 relation between Budgets and Consultants: a Budget has a Consultant and a Consultant can have a Budget.

The code generated by Linq to Sql for the Consultant property of a Budget is as follows:

[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Consultant_Budget", Storage="_Consultant", ThisKey="Id", OtherKey="Id", IsForeignKey=true, DeleteOnNull=true, DeleteRule="CASCADE")]
public Consultant Consultant
{
    get
    {
        return this._Consultant.Entity;
    }
    set
    {
        Consultant previousValue = this._Consultant.Entity;
        if (((previousValue != value) 
                    || (this._Consultant.HasLoadedOrAssignedValue == false)))
        {
            this.SendPropertyChanging();
            if ((previousValue != null))
            {
                this._Consultant.Entity = null;
                previousValue.Budget = null;
            }
            this._Consultant.Entity = value;
            if ((value != null))
            {
                value.Budget = this;
                *this._Id = value.Id;*
            }
            开发者_StackOverflow社区else
            {
                *this._Id = default(int);*
            }
            this.SendPropertyChanged("Consultant");
        }
    }
}

This seems wrong to me: why does the code change the ID of a Budget (see lines in italic) if its Consultant property is set?

By the way: the tables have been built using Lightswitch.


Because Budget depends on the consultant, _Id will contain the ID of the Consultant. When you look into the database, Budget table will contain Id column with primary key constraint and foreign key constraint referencing table for Consultant.

0

精彩评论

暂无评论...
验证码 换一张
取 消