Update to database does not save

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?

Brian Peach Asked on May 4, 2014 in No Category.

This question was posted in Stack Exchange

Add Comment
  • 1 Answer(s)

    I suppose you have one entity Article in your project. After all, having just id’s of articles and not articles themselves is not that usefull. The point is that this model of yours have just one list of ints, the list of id’s and not the list of the articles.

    Entity Framework Code First is convention based, so to establish relationships between entities you add reference between the entities themselves and not their id’s. Think for a while, you have a list of ints, how EF could know these ints correspond to id’s referencing other entities? It’s not like that.

    If you have this, instead:

    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<Article> Articles { get; set; }
    
        public Customer()
        {
            this.Articles = new List<Article>();
        }
    }
    

    Then to add an article you would add the entity itself using the add method from the list collection. In your case, the addition method you write seems superflous. You are using it to avoid null reference exceptions, but if you instantiate the list on the constructor there will be no problems. You would need an adition method if there were some business logic on the addition of an article.

    Doing things in this way you are sticking to EF conventions. When you add an article, EF will know what it must be done. Try doing this.

    Answered on May 4, 2014.

    Hi, While I agree that what you are saying is good practice and probably the correct way it should be done. However it doesnt answer the question as to why I cannot add to a list of ints and update the database

    on May 4, 2014.

    The reason it doesn’t work is simply that EF doesn’t know how to map that list of integers. Let EF build the database for you with the model you presented and then look at the generated database. There will be no place to store that.

    on May 4, 2014.
    Add Comment

    Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.