How I can do just this ( a.myFavorits.Add()
) without pulling the all object to var a
, because a
has a lot of data, and I don\'t
I'm guessing this is what you want? I don't see you 'editting' I only see you adding.
using (var db = dataBase())
{
var a = new user();
....
//set properties etc..
...
a.myFavorits.Add(new BE.FavoritsUsersLong { myLong = f });
db.users.Add(a);
db.SaveChanges();
}
Here's a concrete example that does what you want. In this example, only the Name
of a Company
is modified and saved. Or an item is added to one of its collections.
var cmp = new Company{ CmpId = 1, Name = "Cmp1" }; // CmpId is the primary key
db.Companies.Attach(cmp);
db.Entry(cmp).Property(c => c.Name).IsModified = true;
// Or add an entity to a collection:
cmp.Users = new[] {new User { Name = "a1", PassWord = "a1" } };
try
{
db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();
}
finally
{
db.Configuration.ValidateOnSaveEnabled = true;
}
Result in SQL:
DECLARE @0 VarChar(30) = 'Cmp1'
DECLARE @1 Int = 1
UPDATE [dbo].[Company]
SET [Name] = @0
WHERE ([CmpId] = @1)
There are a few things to note here: