0
votes

I'm writing Api with Visual Studio, but I get errors.

Error 1: error 1

Error 2: error 2

Login login = new Login();
//set some properties
db.Logins.Add(login);
db.SaveChanges();

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

And:

var user = db.Logins.Where(x => x.UserName == username && 
                                x.Password == password).FirstOrDefault();

'object' does not contain a definition for 'UserName' and no extension method 'UserName' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Please help me.

1
Can you post the code of the db? Is it a DbContext? Please right-click go to definition at Logins and show us that code as well.Stefan
I think db.Logins is not a DbSet<Login>. If it was it would work, so please show the code of db as well.Stefan

1 Answers

1
votes

IEnumerable is an interface that contains a single method Enumerator GetEnumerator();. Which makes IEnumerable read-only.

Use ICollection to Add item in a collections.

Check if you import this namespaces:

System.Collections.Generic;

System.Linq;