简体   繁体   English

使用 Dapper 语句的实体框架

[英]Entity framework using statement with Dapper

All of my existing using statements look like the following:我所有现有的 using 语句如下所示:

using (var context = new AppContext())
{
}

Now what if I want to use Dapper to pass a sql string directly to the database rather than:现在,如果我想使用 Dapper 将 sql 字符串直接传递给数据库而不是:

context.Database.SqlQuery(sql);

After looking at the Dapper documenation it looks like it just uses a regular connection string.查看 Dapper 文档后,它看起来只是使用常规连接字符串。 I am unsure of how to adjust my using statement declaration.我不确定如何调整我的 using 语句声明。

I want to be able to do the following like all of the Dapper examples show:我希望能够像所有 Dapper 示例显示的那样执行以下操作:

connection.Query(sql);

I am really hoping dapper will help me with populating custom properties.我真的希望 dapper 能帮助我填充自定义属性。 For example I used a partial class to extend one of my entity framework properties.例如,我使用分部类来扩展我的实体框架属性之一。 Currently when I use pass a sql query through linq this property does not get populated even though it is in the Select.目前,当我使用通过 linq 传递 sql 查询时,即使它在 Select 中,也不会填充此属性。

只需使用 Dapper 的context.Database.Connection.Query<T>() ,因为它只是DbConnection的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM