Querying SQL Server xml column with user provided XPath via Entity Framework

后端 未结 2 1065
执念已碎
执念已碎 2021-01-05 22:25

I\'m having a really tough time figuring how to use an xml data column in SQL Server, specifically for use with Entity Framework.

Basically, one of our tables stores

2条回答
  •  我在风中等你
    2021-01-05 22:58

    To remain "customisable", the SqlQuery method on DbSet can be used:

    var query = @"SET ARITHABORT ON; 
                  select * from [YourTable] where 
                  [xmlcol].exist('/path1/path2[0][text()=''{0}''']";
    var numOfResults = 5;
    var offsetPage = 1;
    
    var results = Context.YourTable.SqlQuery(String.Format(query,"valuetest"))
                                  .OrderBy(x => x.col)
                                  .Skip(offsetPage * numOfResults)
                                  .Take(numOfResults).ToList();
    

    Note, due to its dynamic nature, this method would also most likely expose some degree of sql injection security holes.

提交回复
热议问题