Moe
Moe

Reputation: 63

Linq to sql behavior , context objects , DB relationtion

I am using linq to SQL , I have two problems.

this is a snap shot from my DBML file enter image description here

first one :- in WorkflowInstance table when i call WFTypeID property or any relationship its give me the int? field not the object , not like in ApplicationForm table its working fine. i check the designer.cs file , its keep defining them System.Data.Linq.Mapping.ColumnAttribute not System.Data.Linq.Mapping.AssociationAttribute any idea why this is happening to some tables and some not ?!

second issue :- some tables have 1:N relations and some 1:1 relation. all tables displaying the relationship as EntityRef or EntitySet. why its not giving me a single object. or this is a normal behavior ?!

Upvotes: 1

Views: 176

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062492

1: LINQ-to-SQL exposes a dual API: foreign keys are often presented both in their raw form, and as the object form. So I would fully expect WFTypeID to be an int? or similar. The interesting questions is: is there also a WFType property? If not, you might want to check the association properties. Click on the dotted line, and look at the Child / Parent properties, and check the Cardinality.

2: it is normal for the backing field in a {0|1}-* relationship to be EntityRef<T>, however - the public API generated by LINQ-to-SQL should be just the T; for example:

[global::System.Data.Linq.Mapping.AssociationAttribute(Name="AssociationName", Storage="_User", ThisKey="UserId", OtherKey="Id", IsForeignKey=true)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=7, EmitDefaultValue=false)]
public User User
{
    get
    {
        if ((this.serializing 
                    && (this._User.HasLoadedOrAssignedValue == false)))
        {
            return null;
        }
        return this._User.Entity;
    }
    set
    {
        if ((this._User.Entity != value))
        {
            this.SendPropertyChanging();
            this._User.Entity = value;
            this.SendPropertyChanged("User");
        }
    }
}

where the member-name comes from the association's "Parent Property" > "Name":

enter image description here

Note that _User is EntityRef<User>, but the public property User is of type User.

Upvotes: 2

daryal
daryal

Reputation: 14919

first issue: WFTypeID field is representing a foreign key and I guess your primary keys are integers; it is normal to return integers.

second issue: if the relation is 1 to 1; you will get an EntityRef object representing the object on the other end of the relation. If the relation is 1:N, you will get an EntitySet representing the objects on the other hand. This is normal behaviour, in a one to many relation, one entity will map to "many" entities, not a single object.

Upvotes: 0

Related Questions