0

Can anyone tell where I make a mistake ? :( I want to insert a row using this. It's just not working. I also tried to use "context.SaveChanges();" but nothing changed. No insert at all, and no exception.

public List<string> Add_Address(string address, int selected_item)
        {
            List<string> list = new List<string>();

            using(var context = new RSS_Reader_Database())
            {
                Address Address = new Address();
                Category_Address Category_Address = new Category_Address();

                Address.URL = address.ToString();                               

                int max_id = Convert.ToInt32(context.Addresses.OrderByDescending(t => t.ID_Address).FirstOrDefault());

                Category_Address.ID_Address = max_id;                
                Category_Address.ID_Category = selected_item+1;


                var select_query = from t in context.Addresses select t.URL;


                foreach (var element in select_query)
                {
                    list.Add(element);
                }

            }            

            return list;
}

Edit: Following all Your advices, I made something that works. Looking at this code above, I have no idea what I was trying to do yesterday. Thanks a lot.

public List<string> Add_Address(string address, int selected_item)
        {
            List<string> list = new List<string>();

            using(var context = new RSS_Reader_Database())
            {
                Address Address = new Address() { URL = address };                

                context.Addresses.Add(Address);
                context.SaveChanges();

                int max_id = context.Addresses.Max(u => u.ID_Address);

                Category_Address Category_Address = new Category_Address() { ID_Address = max_id, ID_Category = selected_item + 1 };

                context.Categories_Addresses.Add(Category_Address);
                context.SaveChanges();

                var query = from t in context.Addresses
                            select t.URL;

                var data = query.ToList();

                foreach (var element in data)
                {
                    list.Add(element);   
                }               

            }            

            return list;

        }
6
  • 1
    You don't have any code that inserts anything. Commented Oct 27, 2015 at 21:38
  • Also, use .ToList() Commented Oct 27, 2015 at 21:38
  • Address.URL = address doesn't do that ? Commented Oct 27, 2015 at 21:39
  • You set a property in an object, but you didn't do anything with the object. Commented Oct 27, 2015 at 21:39
  • so I have to make new variable with .ToList() ? Commented Oct 27, 2015 at 21:39

3 Answers 3

1

Saving with Entity Framework generally works like this. Using your above code as a starting point.

using(var context = new RSS_Reader_Database())
{
    Address address = new Address();

    // Set address properties

    context.Addresses.Add(address);
    context.SaveChanges();
 }

You need to add the object to the DbSet<T> where T is the type of the entity that is defined on the DbContext. You then need to call SaveChanges() on the context.

I would suggest reading this. It is an easy to follow introduction to Entity Framework.

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure exactly what you are trying to do.

But if you are expecting to insert the data by the list.Add(element); command it won't work.

If you are planning to insert data into the same DB, you need to use one property from the context to represent the List collection add a new element on this property.

Something like:

context.Lists.Add(element);

Comments

0

if you want retrieve data, you should not call SaveChanges() !,

try get all values from one query like this:

            List<string> select_query = (from t in context.Addresses select t.URL).ToList();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.