2

I am working on an ASP.NET Web Forms project. Due to the specifics of the project most of the data is in XML files, and when I return data from some view I have only a string value of the Entity type that I'm working with. Or to make it more clear, I return json where I have something like {entity : "Clients"} and that's the only way for me to know that I'm working with Clients at the moment.

So we still try to hang on to Entity Framework 5 so in the XML files we store our DB queries, but the problem is that when I want to execute :

        using(var db = new MyContext())
        {
            var allRecords = db.Database.SqlQuery<???>("SELECT * FROM Clients");

I don't have the type as real type to give to the SqlQuery method. So the options that I think I can consider here are :

  • Using classes to match the data - the most common answer when it comes to this problem but in my case the extensive use of XML files would be pointless.
  • Using some sort of reflection to cast my string value to the actual Entity type since I have them in POCOs (I'm using DataBase first approach). I saw some pointers on how eventually this could be done here but to be honest it's pretty complicated for me and I'm not even sure if this will work in that particular scenario, but if you think it's a good way to go please mention it.
  • I was wondering if I can use dynamic somehow like so SqlQuery<dynamic> - it's returning correctly all the records from the database but I can't extract any data from that. I guess with dynamic I still have to cast the result at some point. Not sure, maybe someone knows a way?
  • And last. We really want to use Entity Framework but this is just the starting phase of the project trying things out, seeing how things work together and I'm starting to think that maybe Entity Framework is just not the right tool in this situation. I got this brief answer here and it really made me think about if I should put effort into twisting something when there's just a better approach doing it.
Community
  • 1
  • 1
Leron
  • 9,546
  • 35
  • 156
  • 257
  • Do you have a backing model of any kind or is everything coming from the xml? – jamesSampica Jul 22 '14 at 18:49
  • @Shoe I use `ADO NET Entity Data Model` to create my model from the SQL Database so I have `edmx` schema with `DbContext` and all my Entities created. – Leron Jul 22 '14 at 18:52
  • And what if you use the non generic overload: `dynamic allRecords = db.Database.SqlQuery(theCurrentType, "SELECT * FROM Clients")`? – Pragmateek Jul 22 '14 at 19:46

0 Answers0