1

I have string in xml format with database table name and column description (like column name and data type) and I need to use it for create model(which will map the table) or modify model according the string at runtime. What is the best solution to do this?

When application is running, I need to access some table, with some columns, that can be changed in time, so i need to adapt model for this table.

Please recommend me the technique via I can reach the working solution. Thanks

My data: I have this in string var:

<xml>
 <col>
  <name>OS</name>
  <type>string</format>
 </col>
 <col>
  <name>Name</name>
  <type>string</format>
 </col>
 <col>
  <name>Number</name>
  <type>int</format>
 </col>
</xml>

This xml string I obtain after some user action. So, I need to use it for access to table in .NET MVC. I use NHibernate, so I need to have model which map the table(name of the table is contained in my xml string) and then add NHibernate mapping for this model, I think. But I don't know, how to do it by some "clear" way

mrfazolka
  • 780
  • 1
  • 7
  • 24
  • You mean [deserialization](https://msdn.microsoft.com/en-us/library/ms731073%28v=vs.110%29.aspx)? – Loetn Feb 25 '15 at 09:13
  • My question was updated, I hope it will be more clear now – mrfazolka Feb 25 '15 at 09:28
  • so you actually have a table name? i mean you could map a dataTable to the contents of a sql query.... and use a datatable as your dynamic modelclass – Florian Schmidinger Feb 25 '15 at 09:47
  • I know table name and all table structure info(at runtime). But i don't know what do you mean by "use a datatable as your dynamic modelclass" :) I haven't done this kind of work before – mrfazolka Feb 25 '15 at 09:50
  • Hmm, so if i understand, it mean execute sql command and fill data to DataTable. Then I can iterate DataTable in view and show data from DataTable? It could solve my problem, but can i automatize it using NHibernate's ability to auto generate sql commands and mapping results to models? – mrfazolka Feb 25 '15 at 10:02
  • I think I don't have problem with recompile ..... I tried to do some solution inspirated by this: http://stackoverflow.com/questions/3862226/dynamically-create-a-class-in-c-sharp But I don't know if it good way or not – mrfazolka Feb 25 '15 at 10:09
  • To be honest i once modified a nhibernate project ... only once, only a little and that was 2 years ago. since then i only did entity framework ... with entity framework its a few clicks to update the model if you use database first... – Florian Schmidinger Feb 25 '15 at 10:39
  • That's not problem. Forget to NHibernate for a moment and let find solution for Entity Framework for example. I need to update model, but I need to do it in runtime. Can it be done? – mrfazolka Feb 25 '15 at 11:12
  • you should start put @username in front of your comments you are lucky i checked in again... no Entity Framework is an ORMapper just like Nhibernate ... the only gain is that you can update models a lot easier and then would again have to recompile... not quite sure why it's so important to do this at runtime when you code clearly depends on the model's classes (generating specific sql queries to start with). – Florian Schmidinger Feb 25 '15 at 11:48
  • @FlorianSchmidinger Thanks. So I have 2 questions. How to update models, when i obtain xml string with table info when application is running? And it's not important to do it at runtime, it is just my thought how it could be solved. If it need not to be done at runtime, how to do it? – mrfazolka Feb 25 '15 at 13:02
  • @FlorianSchmidinger btw I've just tried your suggested solution - use DataTable, and it works great. But it means that i will be forced to create all update and insert sql queries manually. However, it's still better than nothing :)) – mrfazolka Feb 25 '15 at 13:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71669/discussion-between-florian-schmidinger-and-mrfazolka). – Florian Schmidinger Feb 25 '15 at 13:03

0 Answers0