首页 > 代码库 > Entity framework DB first---(1)

Entity framework DB first---(1)

Db first 系列-1

1) 创建Db 文件,并指定connectionstring name:

 public class Db : DbContext    {        public Db()            : base("prodinner")        {        }        public DbSet<Country> Countries { get; set; }        public DbSet<Chef> Chefs { get; set; }        public DbSet<Meal> Meals { get; set; }        public DbSet<Dinner> Dinners { get; set; }        public DbSet<User> Users { get; set; }        public DbSet<Role> Roles { get; set; }        public DbSet<Feedback> Feedbacks { get; set; }    }

2) 调用 Db class in your controller

  public ActionResult Index()        {            var result = from d in dbContext.Dinners                         select d;            return View(result);        }

3) 在web.config 中添加Connectionstring

 <add name="prodinner" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=DB name;Integrated Security=True"      providerName="System.Data.SqlClient" />