开发者

SaveChanges() doesn't work?

开发者 https://www.devze.com 2023-01-28 08:31 出处:网络
I have the following code: User user = new User(); user.Name = \"ABC\"; user.Age = \"12\"; _context.AddToUser(user);

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 wh开发者_高级运维en 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!


you forgot:

_context.AddToMeta(meta);
0

精彩评论

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