ByulTaeng
ByulTaeng

Reputation: 1269

SaveChanges() doesn't work?

I have the following code:

User user = new User();

user.Name = "ABC";
user.Age = "12";

_context.AddToUser(user);
_context.SaveChanges();

Meta meta = new Meta();

meta.UserID = user.ID;
meta.Info = "User Info";

_context.AddToMeta(meta);
_context.SaveChanges();

User.ID is Identity and is set to increase by 1 when a new record was inserted. When I run the above code. I got a new user ID by using user.ID but there is no new record was added in User table.

If I comment the last _context.SaveChanges() then the new record is added. I'm new with EF, I've searched for a while but still don't know why.

Any helps would be appreciated!

Upvotes: 1

Views: 868

Answers (1)

Ali Tarhini
Ali Tarhini

Reputation: 5358

you forgot:

_context.AddToMeta(meta);

Upvotes: 4

Related Questions