I am working on an ASP.NET MVC application and I'm using entity framework and linq.
I am having an issue were when I try to update a record it does not update in the database.
This is the class I'm working with:
public class Customer
{
[Key]
[Required]
public int Id { get; set; }
[Required]
public string firstName { get; set; }
[Required]
public string surname { get; set; }
[Required]
public string userName { get; set; }
[Required]
public string password { get; set; }
[Required]
public bool subscription { get; set; }
public List<int> Articles { get; set; }
public void AddArticle(int id)
{
if (Articles != null)
{
Articles.Add(id);
}
else
{
Articles = new List<int> {id};
}
}
}
I have created a new entry based on the above class and saved it to the database and this has worked fine. I have left the ListArticles null for the time being.
Now I get the record:
var customer = context.Customers.SingleOrDefault(o => o.Id == 1);
Here the customer.Articles == null
Now I add to it:
customer.AddArtical(0);
Looking into this I can see that it has updated the variable customer now I need to save these updates in the database.
I have tried all the examples in here but none seem to save to the DB Entity Framework 5 Updating a Record
context.Customers.Attach(query);
context.Entry(query).State = EntityState.Modified;
context.SaveChanges();
After this code is finished another area of the project is called and performs the same query:
var customer = context.Customers.SingleOrDefault(o => o.Id == 1);
however the List<int> Articles
is still null.
Any ideas?