0

I'm trying to add a list of objects to a SQL Server database via Entity Framework but I get an error with Add

[HttpPost]
public void Post(List<Row> rows)
{
    try
    {
        using (DbModel dbModel = new DbModel())
        {
            foreach (var el in rows)
            {
                dbModel.Provider_Status.Add(el);
            }

            dbModel.SaveChanges();
        }
    } 
    catch { }
}

Row class:

public class Row
{
    public string FileName { get; set; }
    public string FileTitle { get; set; }
    public string ProviderID { get; set; }
    public string ServiceID { get; set; }
    public string PublishDate { get; set; }
    public string ExpiryDate { get; set; }
}

Database Model DbModel:

    public partial class Provider_Status
    {
        public int Id { get; set; }
        public string FileName { get; set; }
        public string FileTitle { get; set; }
        public string ProviderID { get; set; }
        public string ServiceID { get; set; }
        public string PublishDate { get; set; }
        public string ExpiryDate { get; set; }
    }

Error Message:

CS1503 Argument 1: cannot convert from 'File_Upload.Models.Row' to 'File_Upload.Models.Provider_Status

5
  • What is the error?? And what does your DbModel class look like? Commented Apr 18, 2021 at 9:49
  • I updated the post which have the DbModel and the error message Commented Apr 18, 2021 at 10:01
  • 1
    Maybe you need a type convertor from Row to Provider_Status, because it looks like you're trying to insert Row objects into the Provider_Status collection. Commented Apr 18, 2021 at 10:52
  • I changed the parameter to List<Provider_Status> rows and it worked .. Thank you guys Commented Apr 18, 2021 at 12:18
  • Also take a look at AddRange instead of using a loop Commented Apr 18, 2021 at 20:51

1 Answer 1

1

Your DbModel defines a data set of type Provider_Status - so if you want to add data to this data set, you need to provide Provider_Status objects - not Row objects (as you do now).

You need to convert those Row object to Provider_Status - try something like this:

[HttpPost]
public void Post(List<Row> rows)
{
    try
    {
        using (DbModel dbModel = new DbModel())
        {
            foreach (var el in rows)
            {
                // create a new "Provider_Status" object, based on the
                // "Row" values being passed in
                Provider_Status status = new Provider_Status
                                         {
                                            FileName    = el.FileName 
                                            FileTitle   = el.FileTitle 
                                            ProviderID  = el.ProviderID
                                            ServiceID   = el.ServiceID 
                                            PublishDate = el.PublishDate
                                            ExpiryDate  = el.ExpiryDate
                                         };

                // add that new Provider_Status object to your dbModel
                dbModel.Provider_Status.Add(status);
            }

            dbModel.SaveChanges();
        }
    } 
    catch { }
}
Sign up to request clarification or add additional context in comments.

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.