Does not contain a definition for "Add"

asp.net-mvc asp.net-mvc-4 c# entity-framework

Question

I'm trying to make the same thing like in this thread, but I'm getting error:

'System.Collections.Generic.IEnumerable' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

Here is my code:

[HttpPost]
public ActionResult Create(ANIME anime)
{
    var db = new MainDatabaseEntities();
    var newanime = new ANIME
    {
        ID_AN = anime.ID_AN,
        TITLE_OR = anime.TITLE_OR,
        TITLE_EN = anime.TITLE_EN,
        GENRES = new List<GENRES>()
    };

    foreach (var selectedAnime in anime.GENRES.Where(c => c.isSelected))
    {
        var genre = new GENRES { ID_GE = selectedAnime.ID_GE };
        db.GENRES.Attach(genre);
        newanime.GENRES.Add(genre); <--- this is the error line
    }

    db.ANIME.Add(newanime);
    db.SaveChanges();
    return RedirectToAction("Index");
}

ANIME:

public partial class ANIME
{
    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual IEnumerable<GENRES> GENRES { get; set; }
}

GENRES:

public partial class GENRES
{
    public int ID_GE { get; set; }
    public string GENRE { get; set; }
    public bool isSelected { get; set; }
    public virtual ICollection<ANIME> ANIME { get; set; }
}

The error is in the line newanime.GENRES.Add(genre) in HttpPost. I added using System.Linq to all models and controllers but it doesn't help. Any ideas how to resolve this?

EDIT:

After repairing this a new error arrived. I think it's not related to above one but I don't want to spam unnecessary threads.

Error message:

The entity or complex type 'MainDatabaseModel.GENRES' cannot be constructed in a LINQ to Entities query.

Related code:

public ActionResult Create()
{
    var db = new MainDatabaseEntities();
    var viewModel = new ANIME
    {
        GENRES = db.GENRES.Select(c => new GENRES
        {
            ID_GE = c.ID_GE,
            GENRE = c.GENRE,
            isSelected = false
        }).ToList()
    };
    return View(viewModel);       
}
1
2
5/23/2017 12:26:27 PM

Accepted Answer

Try this:

public partial class ANIME
{

    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual ICollection<GENRES> GENRES { get; set; } // Use ICollection here
}
5
12/2/2013 11:21:58 AM

Popular Answer

You have a IEnumerable property that you're initialising with a List. The List class implements the IEnumerable interface.

When you are invoking something like this:

IEnumerable myList = new List<MyType>();

you're saying that you want for your object the features of the IEnumerable interface that are also inherited in the List class. In this case, the method Add isn't part of the IEnumerable interface, because it's a method of the List class only, and you have that exception.

You have then to change the type of your property, from IEnumerable<YourType> to IList<YourType> (more info about IList here). In this way, the exception about Add method won't be thrown.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow