实体框架。从表中获取所有对象并打印到控制台

时间:2017-10-20 08:02:48

标签: c# entity-framework

我是EF的新手,我在理解这个概念时遇到了一些严重的问题。我有一个与数据库的连接,它似乎工作。我可以添加对象。某种灵魂可以帮助我检索整个表格,然后将其打印到控制台。

这就是它现在的样子;

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

<html><body>
Hello world!
</body></html>