8

I have the following code:

 internal static bool SaveUOSChangeLog(List<Contracts.DataContracts.UOSChangeLog> values, string user)
    {
        try
        {
            using(var ctx = new StradaDataReviewContext2())
            {
                values.ForEach(u => { u.Username = user; u.Changed = DateTime.Now; });
                var test = ctx.UOSChangeLog.Add(values);
                ctx.SaveChanges();
                return true;
            }
        }

The thing I want to do Is to save values to the database. However, I get a the following error message:

enter image description here

Here is my Contracts.DataContracts.UOSChangeLog:

   public int? Id { get; set; }
        public int Accident_nr { get; set; }
        public int Refnr { get; set; }
        public int Action { get; set; }
        public string Old_data { get; set; }
        public string New_data { get; set; }
        public DateTime SearchedFromDate { get; set; }
        public DateTime SearchedToDate { get; set; }
        public DateTime Changed { get; set; }
        public string Username { get; set; }
        public string Comment { get; set; }

And here Is my Services.StradaDataReview2Model.UOSChangeLog that are used as a DbSet

[Table("UOSChangeLog")]
    public partial class UOSChangeLog
    {
        [Required]
        public int? Id { get; set; }

        public int Accident_nr { get; set; }
        [Required]
        public int Refnr { get; set; }
        [Required]
        public int Action { get; set; }
        [Required]
        public string Old_data { get; set; }
        [Required]
        public string New_data { get; set; }
        [Required]
        public DateTime SearchedFromDate { get; set; }
        [Required]
        public DateTime SearchedToDate { get; set; }
        [Required]
        public DateTime Changed { get; set; }
        [Required]
        public string Username { get; set; }
        [Required]
        public string Comment { get; set; }
    }
2
  • your are trying to add a new List of specified class (values is a List) rather than a specific instance Commented Mar 17, 2016 at 13:37
  • @apomene: Hm, okey, and how should I do It then? Commented Mar 17, 2016 at 13:39

3 Answers 3

7

You're trying to add a list with the Add method which takes a single object, just keep it simple and use a foreach:

using(var ctx = new StradaDataReviewContext2())
{
   foreach(var value in values)
   {
       value.Username = user;
       value.Changed = DateTime.Now;
       ctx.UOSChangeLog.Add(value);
   }
   ctx.SaveChanges();
   return true;
}

Just use a simple foreach, linq is a querying language, not a modifying language.

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

8 Comments

@Bryan That's the best option, by using linq foreach you're not actually gaining anything
Hi again, I still get the same error when doing like you do.
@Bryan value is of type UOSChangeLog right? Can you copy the error message?
Here u can find a printscreen on the values of value list: postimg.org/image/56uxs9yzb and here you can find the error message: postimg.org/image/4felk4dir
@Bryan They're of different types. Your values are from Contracts.DataContracts namespace, and your objectcontext is Strada.DataReview... namespace. You'll have to create the right object to add it (of the Strada namespace).
|
7

Please use addrange method.

db.TheTable.AddRange(TheList)
db.SaveChanges();

Comments

4

You can use Entity Framework's .AddRange method to add a collection of objects to your Db.

MSDN

It will look like:

       using(var ctx = new StradaDataReviewContext2())
        {
            values.ForEach(u => { u.Username = user; u.Changed = DateTime.Now; });
            var test = ctx.UOSChangeLog.AddRange(values);
            ctx.SaveChanges();
            return true;
        }

5 Comments

Ah okey. But I Still get same error described In my question.
Stupid question: your context does inherit DbContext, correct?
@DanOrlovsky If I call List.Clear() after AddRange, will the db still have the data?
@Austin_Anderson - so long as you savechanges, you should be OK.

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.