当没有DbSet<>时,如何使用DbContext在里面,为了查询? [英] How to use DbContext when there is no DbSet<> inside that, for query?

查看:221
本文介绍了当没有DbSet<>时,如何使用DbContext在里面,为了查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架代码优先,这是我的 DbContext 。如你所见,没有关于 DbSet<> 属性,所有这些都将由我的模型类提供,由C# CodeDOM 提供。我通过使用代码优先动态创建我的

I'm using Entity Framework Code-First and this is my DbContext. As you see there is nothing about DbSet<> properties and all of that will provide from my model classes which are providing by C# CodeDOM. I'm creating my Tables dynamically by using Code-First.

    public class MyDBContext : DbContext
{

    public MyDBContext() : base("MyCon")
    {
        Database.SetInitializer<MyDBContext>(new CreateDatabaseIfNotExists<MyDBContext>());
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
        var theList = Assembly.GetExecutingAssembly().GetTypes()
                  .Where(t => t.Namespace == "FullDynamicWepApp.Data.Domins")
                  .ToList();
        foreach (var item in theList)
        {
            entityMethod.MakeGenericMethod(item)
                           .Invoke(modelBuilder, new object[] { });
        }
        base.OnModelCreating(modelBuilder);
    }

}

但现在我不知道如何在我的 DbContext 内部没有任何 DbSet 之前写我的查询Linq ?做我的DbContext的实例,并使用哪个属性?

but now I don't know how can I write my query Linq without any DbSet<> inside my DbContext? make instance of my DbContext and use which property?

 MyDBContext Db = new MyDBContext();
        Db.What???????

在这种情况下,如何写我的 CRUD操作? p>

How can I write my CRUD operation s in these circumstances?

推荐答案

DbContext.Set< TEntity> 方法将返回给定类型的 DbSet< TEntity> ,例如:

Db.Set<Entity>().Add(entity);

这篇关于当没有DbSet&lt;&gt;时,如何使用DbContext在里面,为了查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆