开发者

Linq is not persisting addition to a list?

I've got a class T with an association to a cross reference table. I'd like to have a method on the class that adds an entry to the cross reference by receiving an entity representing the other side. For some reason, though, while I can add the item to the collection it doesn't get added to the change set for the data context.

So the class in question lo开发者_运维百科oks like:

class T
{
    public EntitySet<T_U> t_users
    {
        get
        {
            if ((this.serializing && (this.t_user.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
        return this.t_user;
        }
        set
        {
            this.Users.Assign(value);
        }
    }

    public AddUser(U user)
    {
        this.t_users.Add( new T_U() { TID = this.ID, UID = user.ID );
    }
}

The client using this class would basically do something like:

var db = new DBDataContext();
var t = db.Ts.FirstOrDefault( t => t.ID = 100);
var u = db.Us.FirstOrDefault(u => u.ID == 3);

t.AddUser(u);

db.SubmitChanges();

Shouldn't that succesfully add a record to the cross reference table?


I think you want InsertOnSubmit instead of Add. Are you using an outdated version of Linq to sql?


You are adding new T_U to this.Users... Does that work? Isn't that the wrong type?

 this.Users.Add( new T_U() { TID = this.ID, UID = user.ID );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜