1

I have the following problem. When debugging a project where I'm playing around a bit with Code First, the Items are somehow not added to the purchase. The purchases are added to the client.

This is how I seed the Data.

public class PublicDatabaseInitializer : DropCreateDatabaseIfModelChanges<PublicDataContext>
{
    protected override void Seed(PublicDataContext context)
    {
        context.Clients.Add(new Client()
        {
            // ...
            Purchases = new[]
                       {
                           new Purchase()
                           {
                               // ...
                               Items = new[]
                                        {
                                            new Item()
                                            {
                                                // ...
                                            },
                                            new Item()
                                            {
                                                // ...
                                            }
                                        }
                           },
                           new Purchase()
                           {
                               // ...
                           }
                       }
        });
        base.Seed(context);
    }
}

And this is how I declare the two ICollection properties:

public virtual ICollection<Purchase> Purchases { get; set; }
public virtual ICollection<Item> Items{ get; set; }

I would be thankful for any help.

Dänu
  • 5,791
  • 9
  • 43
  • 56
  • Is `Slides` = `Items`, a typo? – Slauma Jul 21 '12 at 10:46
  • Yes it is, sorry I really should pay more attention when copy pasting stuff.. I edited my question – Dänu Jul 21 '12 at 11:02
  • Do you mean with "*the Items are somehow not added to the purchase*" that the `Items` table in the database is empty after `Seed` or that the items *are* in the table but do not have the correct foreign key to the purchase? – Slauma Jul 21 '12 at 11:15
  • This is actually a very good question :-). As I am just getting started with code first, I can't seem to find the Database using SQL Server Management Studio. In the web.config, I specified the following connection string: `` – Dänu Jul 21 '12 at 12:41
  • 1
    Try to remove that "*User Instance*" stuff, it is terrible and very confusing and follow the advice here: http://stackoverflow.com/a/11548688/270591 – Slauma Jul 21 '12 at 12:46
  • Yeah, it was the "User Instance". Works fine now, but that was very strange... Want to write the last comment in an answer? I'll tick it then. – Dänu Jul 21 '12 at 14:09
  • That wasn't really an answer to your question :) Maybe write it as your own answer that the problem was somewhere else (you can accept your own answer) with a link to marc_s' answer (and don't forget to give *him* an upvote if you haven't already, that is the *really* helpful answer). – Slauma Jul 21 '12 at 14:26

2 Answers2

1

You need to explicitly call DbContext.SaveChanges()

// add client stuff
context.SaveChanges();
David Fox
  • 10,603
  • 9
  • 50
  • 80
  • Oops, seems my copy pastry has failed, in the end there I call base.Seed(context), which should then save the changes on the context. See my edit – Dänu Jul 21 '12 at 09:52
0

Thanks to Slauma I found a solution. As mentioned in this post: stackoverflow.com/a/11548688/270591. While I am still not fully clear on the why, the how of the solution is the following:

Remove user instance = true from the connection string.

I guess this has something to do with access rights and / or permissions.

Community
  • 1
  • 1
Dänu
  • 5,791
  • 9
  • 43
  • 56