Is it possible to run native sql with entity framework?

后端 未结 7 1332
北海茫月
北海茫月 2020-11-28 06:12

I am trying to search an XML field within a table, This is not supported with EF.

Without using pure Ado.net is possible to have native SQL support with EF?

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 06:54

    Since .NET 4 you can use ExecuteStoreQuery method:

    var list = myDBEntities.ExecuteStoreQuery(MyClass.sql);
    

    where myDBEntities is inherited from ObjectContext.

    class MyClass
    {
        /* You can change query to more complicated, e.g. with joins */
        public const string sql = @"select [MyTable].[MyField] from [MyTable]";
        public string MyField { get; set; }
    }
    

    Notice that MyTable is real table name, not EF class.

提交回复
热议问题