Get Linq for SQL to save to database

So, I have the following code:

// Get the current user
_currentUser = WindowsIdentity.GetCurrent();

// Get the list of address for the current user
_dataMap = new DataMapDataContext();
_addresses = _dataMap.Addresses
         .Where(address => address.InsertUserName == _currentUser.Name).ToList();

....

_addresses.Add(form.Item);
_dataMap.SubmitChanges();

When I call SubmitChanges, nothing is stored in the database. Why is this? Am I missing a point? I thought that with linq to sql you can just add elements to your queries and then call SubmitChanges and it will work ... Obviously I'm missing something.

Now it works if you use "ToList"? If not, how do you insert material into the collection? (I don't think Add is part of IQueryable.)

+1
source share
1 answer

Do not use _addresses.Add

You cannot add directly to IQueryable, you need to add it to a set of objects.

Try the following:

    _dataMap.Addresses.InsertOnSubmit(form.Item);
    _dataMap.SubmitChanges();

, , form.Item, , Entity, _dataMap.Addresses.

, .ToList().

LINQ-SQL ( - POCO), /, .

.InsertOnSubmit , , .SubmitChanges INSERT .

InsertOnSubmit .

+6

All Articles