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,
AlertCo开发者_开发知识库nfiguration = 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
精彩评论