1

I'm a little bit confused.

I have this Code:

 using(Test_ASLEntities context = new Test_ASLEntities())
            { 
                List<Belege> kopieren_SQL_List = new List<Belege>();

                kopieren_SQL_List = (from b in context.Beleges
                                    select b).ToList();

                var kopieren_SQL = new Belege
                {
                    Plz = 1000,                   
                    Nachname = "Name",
                    Vorname = "Name",
                };
                kopieren_SQL_List.Add(kopieren_SQL);
                context.SaveChanges();
            }

            Console.ReadLine();

And i dont get it, why it dosn't save the changes in m database?!

The context is from a .edmx datamodel entitites.

Can someone tell me what i did wrong there? (I just copyed most of the Code out of a other program that i created for quite some time , and there it worked fine. Thats even more confusing)

Edit: NVM, saw my mistake. Sry :(

3 Answers 3

4

Use context.Beleges.Add(kopieren_SQL) instead of kopieren_SQL_List.Add(kopieren_SQL).

4

You're not adding your new Belege to your datacontext.

Try context.Beleges.Add(kopieren_SQL).

3

Because you have to use DbSet object to signal a new object was added to EF
From MSDN what the Add method do

Adds the given entity to the context underlying the set in the Added state such that it will be inserted into the database when SaveChanges is called.

 var kopieren_SQL = new Belege
                {
                    Plz = 1000,                   
                    Nachname = "Name",
                    Vorname = "Name",
                };
//EF framework will add a plural S to represents an object collection 
context.Beleges.Add(kopieren_SQL); 
//here  commit your changes to the DB 
context.SaveChanges();  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.