0
votes

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

1
What is the error?? And what does your DbModel class look like? - marc_s
I updated the post which have the DbModel and the error message - Abdulaziz Yahya
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. - AlwaysLearning
I changed the parameter to List<Provider_Status> rows and it worked .. Thank you guys - Abdulaziz Yahya
Also take a look at AddRange instead of using a loop - TheMixy

1 Answers

1
votes

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 { }
}