I'm following a EF Core (beginners) course and there is something I don't understand.
Below you can find the most important classes of a little excercise we got, to get to know EF core and the intent.
In the body of the ReadAllStudents method under the Repository class, there is a done return of the Student DbSet with an AsEnumerable. But why should I even place AsEnumerable?
Isn't it already an Enumerable even without the AsEnumerable? with or without the result and the used query is the same...
Thanks for helping me out!
Below you can find the most important classes of a little excercise we got, to get to know EF core and the intent.
Student:
public class Student
{
public int StudentId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public Student(string firstname, string lastname)
{
Firstname = firstname;
Lastname = lastname;
}
}
EfTestDbContext:
public class EfTestDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public EfTestDbContext()
{
EfTestInitializer.Initialize(this, true);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Data Source = EF_TEST.db")
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug()));
}
}
}
EfTestInitializer:
public class EfTestInitializer
{
public static void Initialize(EfTestDbContext context, bool dropAndCreateDatabase = false)
{
if (dropAndCreateDatabase)
{
context.Database.EnsureDeleted();
}
if (context.Database.EnsureCreated())
{
Seed(context);
}
}
private static void Seed(EfTestDbContext context)
{
Student s1 = new Student("M", "S");
Student s2 = new Student("A", "S");
context.Students.Add(s1);
context.Students.Add(s2);
context.SaveChanges();
}
}
Irepository:
public interface IRepository
{
IEnumerable<Student> ReadAllStudents();
}
Repository:
public class Repository : IRepository
{
private EfTestDbContext context;
public Repository()
{
context = new EfTestDbContext();
}
public IEnumerable<Student> ReadAllStudents()
{
return context.Students.AsEnumerable();
}
}
Program:
class Program
{
private static readonly IRepository Repository = new Repository();
static void Main(string[] args)
{
ShowAllStudents();
}
private static void ShowAllStudents()
{
Console.WriteLine(Repository.CountAllStudents());
}
}
In the body of the ReadAllStudents method under the Repository class, there is a done return of the Student DbSet with an AsEnumerable. But why should I even place AsEnumerable?
Isn't it already an Enumerable even without the AsEnumerable? with or without the result and the used query is the same...
Thanks for helping me out!