0

im just creating my first MVC applicaiton and am having trouble connecting to my database located on my sql server.

i have added the connection string to the web config as normal, created a model with all the fields in.

i created a model and created a new DBContext as there wasnt one listed. this created the below file

im not sure how it connects to the right table in my SQLDB, how do i do this? also how do i make it run stored procedures?

Thanks

public EquipmentDBContext()
            : base("name=ITAPPConnectionString")
        {
        }

        public DbSet<Equipment> Equipments { get; set; }
4

2 回答 2

0
public EquipmentDBContext()
            : base("name=ITAPPConnectionString")//this name should be the name of database
        {
        }

        public DbSet<Equipment> Equipments { get; set; }

在这里,您说您有一个名为 Equipment 的数据模型。您的上下文还定义了一个 DbSet 类型的属性 Equipments。此属性充当一个集合,允许您查询数据库中表中的数据,就好像它是内存中的对象集合一样。

因此,如果您在名为db的控制器中创建类EquipmentDbContext的对象,那么您可以使用类似的方式访问表中的数据

db.Equipments
于 2013-07-12T10:16:43.247 回答
0

要进一步扩展 Cyber​​cop 的答案,你会做这样的事情

using (var context = new EquipmentDBContext())
{
    var equipments = context.Equipments.ToList();
    var equipment = context.Equipments.FirstOrDefault(c=>c.Id == 1);
    var blueThings= context.Equipments.Where(c=>c.Color == "blue").ToList();
}
于 2019-06-14T18:08:44.110 回答