Reputation: 29
I am new to EF and im having some serious problems understanding the concept. I have a connection to database and it seems to work. I can add objects. Could some kind soul help me out with retrieving the whole table and then print it to the console.
This is how it looks right now;
namespace EntityFrameWorkExample
{
class Program
{
static void Main(string[] args)
{
SampleDBEntities3 samp = new SampleDBEntities3();
Cars c = new Cars();
c.Brand = "Ford";
c.Model = "P200";
samp.Cars.Add(c);
samp.SaveChanges();
using (var db = new SampleDBEntities3())
{
}
}
}
}
namespace EntityFrameWorkExample
{
using System;
using System.Collections.Generic;
public partial class Cars
{
public int CarID { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
}
}
namespace EntityFrameWorkExample
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class SampleDBEntities3 : DbContext
{
public SampleDBEntities3()
: base("name=SampleDBEntities3")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Cars> Cars { get; set; }
}
}
Thanks in advance!
Upvotes: 1
Views: 3939
Reputation: 11
var info=DbSet<Cars>().ToList();
foreach(var a in info)
{
Console.Writeline(a.Brand);
Console.Writeline(a.Model);
Console.Writeline(a);
}
Upvotes: 1