Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework SaveChanges()不起作用?_Entity Framework_Savechanges - Fatal编程技术网

Entity framework SaveChanges()不起作用?

Entity framework SaveChanges()不起作用?,entity-framework,savechanges,Entity Framework,Savechanges,我有以下代码: 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

我有以下代码:

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是标识,在插入新记录时设置为增加1。当我运行上面的代码时。我使用user.ID获得了一个新的用户ID,但在用户表中没有添加新记录

如果我对最后的_context.SaveChanges进行注释,则会添加新记录。我是EF的新手,我已经搜索了一段时间,但仍然不知道为什么

任何帮助都将不胜感激

你忘了:

_context.AddToMeta(meta);
你忘了:

_context.AddToMeta(meta);

我漏掉了那一行,但在用户表中没有添加新记录,而不是元表。可能用户上有一个应该设置的外键属性我漏掉了那一行,但在用户表中没有添加新记录,而不是元表。可能用户上有一个应该设置的外键属性