使用DbSet< TEntity> .SqlQuery(sqlcmd,params) [英] Using DbSet<TEntity>.SqlQuery(sqlcmd, params)

查看:52
本文介绍了使用DbSet< TEntity> .SqlQuery(sqlcmd,params)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个在CTP5中使用此功能的好例子?


据我所知,此功能允许将SqlQuery的结果具体化为被跟踪的对象实例由DbContext通过DbSet上的SqlQuery方法,而DbContext.Database.SqlQuery< TEntity>没有。


DbContext.Database.SqlQuery< TEntity>的一般示例是:


        static void Main(string [] args)

        {

           使用(MyContext db = new MyContext())

            {

               串SQLCMD = QUOT; SELECT * FROM myTable的"




&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; //查询对myTable的所有项目




&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; IEnumerable的< myEntity所>的SQLQuery = db.Database.SqlQuery< myEntity所>(SQLCMD);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;的foreach(在的SQLQuery myEntity所项)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Console.WriteLine(" - {0} - {1} - {2}",item.field0,item.field1,item.field2);

     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


                Console.WriteLine("按任意键继续。");
$
              &NBSP;&NBSP; Console.ReadKey();
$




           &NBSP; }

解决方案

Cborcher,


 


这里是一个使用DbSet<> .SqlQuery的示例。 
假设您已定义以下实体和上下文:


< span style ="font-family:Calibri; font-size:small"> 


   
public class
产品


   
{


       
public int Id {
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string 名称{
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public decimal 价格{
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public int CategoryId {
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public 类别类别{
获得; set ; }


&NBSP;&NBSP;&NBSP;
}


 


&NBSP;&NBSP;&NBSP;
public class
类别


   
{


       
public int Id {
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string 名称{
获得; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public ICollection < 产品>产品{
获取; set ; }


&NBSP;&NBSP;&NBSP;
}


 


&NBSP;&NBSP;&NBSP;
public class
ProductsContext :<跨度风格=" 颜色:#2b91af ">
的DbContext


   
{


       
public DbSet < 产品>产品{
获取; set ; }


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public DbSet < 分类>类别{
获取; set ; }


&NBSP;&NBSP;&NBSP;
}


 


默认情况下,Product实体将映射到数据库中名为Products的表。 
您可以使用SqlQuery查询和跟踪所有产品,如下所示:


 


   
使用 var context =
ProductsContext ())


   
{


       
列表< 产品>产品= context.Products.SqlQuery(的" SELECT * FROM产品" )。ToList();


   
}


 


希望这会有所帮助。


 


谢谢,


亚瑟


Does anyone have a good example of using this feature in CTP5?

As I understand this feature allows the results of the SqlQuery to be materialized into object instances that are tracked by the DbContext via the SqlQuery method on DbSet while the DbContext.Database.SqlQuery<TEntity> does not.

A generic example of the DbContext.Database.SqlQuery<TEntity> is :

        static void Main(string[] args)
        {
            using (MyContext db = new MyContext())
            {
                string sqlcmd = "SELECT * FROM myTable"


                // Query for all items in myTable


                IEnumerable<MyEntity> sqlQuery = db.Database.SqlQuery<MyEntity>(sqlcmd);
                foreach (MyEntity item in sqlQuery)
                {
                    Console.WriteLine(" - {0} - {1} - {2}", item.field0, item.field1, item.field2);
                }

                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();


            }

解决方案

Cborcher,

 

Here’s an example of using DbSet<>.SqlQuery.  Let’s assume you have the following entities and context defined:

 

    public class Product

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public decimal Price { get; set; }

        public int CategoryId { get; set; }

        public Category Category { get; set; }

    }

 

    public class Category

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public ICollection<Product> Products { get; set; }

    }

 

    public class ProductsContext : DbContext

    {

        public DbSet<Product> Products { get; set; }

        public DbSet<Category> Categories { get; set; }

    }

 

By default the Product entity will be mapped to a table called Products in the database.  You can use SqlQuery to query for and track all products like so:

 

    using (var context = new ProductsContext())

    {

        List<Product> products = context.Products.SqlQuery("select * from products").ToList();

    }

 

Hope this helps.

 

Thanks,

Arthur


这篇关于使用DbSet&lt; TEntity&gt; .SqlQuery(sqlcmd,params)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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