.SaveChanges不适用的实体框架更改数据库 [英] .SaveChanges not applying changes to database in entity framework
问题描述
我尝试使用下面的code在我的分贝刚刚用户链接到alertconfigurations了UserAlertSubscriptions表中添加一个条目。
现在的问题是,虽然我没有得到任何错误项不被添加到数据库中。
知不知道我做错了。正如你所看到的,我试过ApplyAllChanges,但是这并没有区别。
AlerterDataEntities alerterDataEntities =新AlerterDataEntities()
//获取当前用户
用户USER = alerterDataEntities.Users.Where(U => u.UserID == 1).ToList<使用者>()[0];
//获取选定的警报配置
AlertConfiguration alertConfiguration =
alerterDataEntities.AlertConfigurations.Where(一个=> a.AlertConfigurationID == 3).ToList
&其中; AlertConfiguration>()[0];
UserAlertSubscription userAlertSubscription =新UserAlertSubscription
{
用户=用户,
AlertConfiguration = alertConfiguration
};
user.UserAlertSubscriptions.Add(userAlertSubscription);
//alerterDataEntities.AcceptAllChanges();
alerterDataEntities.SaveChanges();
所有固定的 - 我发现这个问题是数据库已被我跑了,每次复制到输出目录。所以我一直在寻找(一个在我的应用程序的根目录)的表不是一个,它已添加到。
本帖由@让 - 路易斯是在那里我找到了解决办法解决: C#Entitity框架调用SaveChanges()不工作
要解决它,我将数据库设置复制到输出目录到不要复制,把数据库连接字符串中完整的文件路径在App.config
I'm trying to use the code below to add an entry in the 'UserAlertSubscriptions' table in my db which just links users to alertconfigurations.
The problem is that although i get no errors the entry does not get added to the database.
Any idea what i'm doing wrong. As you can see, I tried ApplyAllChanges, but this made no difference.
AlerterDataEntities alerterDataEntities = new AlerterDataEntities()
// get current user
User user = alerterDataEntities.Users.Where(u => u.UserID == 1).ToList<User>()[0];
// get selected alert configuration
AlertConfiguration alertConfiguration =
alerterDataEntities.AlertConfigurations.Where(a => a.AlertConfigurationID == 3).ToList
<AlertConfiguration>()[0];
UserAlertSubscription userAlertSubscription = new UserAlertSubscription
{
User = user,
AlertConfiguration = alertConfiguration
};
user.UserAlertSubscriptions.Add(userAlertSubscription);
//alerterDataEntities.AcceptAllChanges();
alerterDataEntities.SaveChanges();
All fixed - I found the problem was that the database was being copied to the output directory each time I ran it. So the table I was looking at (the one in the root of my application) was not the one it had added to.
This post solved by @Jean-Lois was where I found the solution: C# Entitity framework SaveChanges() not working
To solve it I set the database 'Copy to output directory' to 'Do not copy' and put the full file path in the database connection string in the app.config
这篇关于.SaveChanges不适用的实体框架更改数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!