0

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?

Community
  • 1
  • 1
brian4342
  • 1,265
  • 8
  • 33
  • 69

1 Answers1

0

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.

user1620696
  • 10,825
  • 13
  • 60
  • 81
  • 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 – brian4342 May 04 '14 at 13:28
  • 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. – user1620696 May 04 '14 at 15:02