MVC4 : How to create model at run time?

In my project I am dynamically creating table by giving table name (ex. student) and adding fields to that table and then save table. Now, my table is created in SQL Server database.

Assume table has data and I want to show the data of table (ex student) on WebGrid. But, the problem is I have no model for that table (ex. student). So, how I can create model for newly created table or how I can show the the data on WebGrid?

6

Don’t do that

You really shouldn’t be modifying table schema at runtime. All sorts of things can go wrong. You could have someone try to create a column that turns out to be a reserved word. The access you are giving the user to be able to do these changes allows for things like dropping tables too.

You really shouldn’t be modifying table schema at runtime. It means you are not using much of what a database is good at.

Try an EAV table structure instead

If you are going to be writing dynamic data where columns change and are arbitrarily created, consider using an EAV table structure instead. It is well known and understood how it works.

Yes, various DBAs and purists will fuss at at it, but will you suggest your proposed solution of adding columns at runtime, they will probably give in. You can even find libraries that will let you write a model for it (such as EAV @ CodePlex).

Maybe try something out of NoSQL

Maybe you don’t want a relational database at all. Maybe you want something that has a document model and you’re just sticking json in there. Then mongo or couch would work acceptably. Or maybe you want a column oriented database like Cassandra. I’m not sure, but its something to explore if you really are after that ‘creating columns at runtime’ type thought.

Ok, you’re really doing that

You can get the metadata from a table back. In the Java world, this would be something like ResultSetMetaData which lets you find out the specifics of all of the columns, the types of data they store and other bits about it.

In C#, apparently this is stored in the DataColumn class. From Retrieving metadata (table name) from a SQL statement on Stack Overflow:

SqlConnection con = new SqlConnection(connString);
String queryString = "Select CUSTOMER_NAME from CUSTOMER_DETAIL";
SqlCommand cmd = new SqlCommand(queryString, con);
DataTable myTable = new DataTable();
myTable.Load(cmd.ExecuteReader());
DataColumn column = myTable.Columns[0];
// zero based index of column, alternatively use column name
string typeOfColumn = column.DataType.Name;
// or column.DataType.FullName to get the fully qualified name of the System.Type

If you don’t know what’s in a table, you shouldn’t be trying to display it anywhere! What if it’s full of credit card details (Yowch!) or passwords in “anonymously-named” fields (Eek!)?

And you just plump them all up on the screen for all to see?
To me, that’s bordering on irresponsible at the very least.

You need to know what’s in there before you try doing anything with it.

You say you’re creating field dynamically in your database. OK; it’s not a Good way to design your database in my book, but it’s A way. I’d suggest you create metadata tables – just more tables in your database – that describe the real tables that your application currently uses. You’ll need (at least)

  • one metadata table that describes each real table and
  • another metadata table that describes each column within each real table.

This metadata can be queried at run time and [accurately] describes each table and field in the database, what it contains and whether you’re allowed to display it, amongst other things. It also documents your database “design”, even though it’s created “in-flight”.

The important thing is that this is all built in to whatever U.I. you use to build your real tables – you need to be filling this stuff in (“writing it down”) before you add the field into the database.

Not the best solution, but should work.

To display table data, only input you have is name of the table. So you will do

string sql = "SELECT * from " + tableName;

And then fill a DataSet and then convert DataSet to List<List<string>> object like in this answer. Pass that on to your view. Loop through it in the view to display tabular data. The caveat here is that you will handle all datatypes as string.

I don’t think you will be able to use WebGrid

4

Alternative approach using PostgreSQL in bio-medical data: decomposed storage.

Idea revolves around creating a separate (object, field) table for each field and carefully constructing queries to optimize the data retrieval. So if you have object:

Student {id, name, dob, country}

You would create 4 tables:

  • student (id)
  • student_name (id, name)
  • student_dob (id, dob)
  • student_country (id, country)

The advantage compared to EAV design is that data retrieval and storage logic is a bit simpler, while maintaining small data footprint. Disadvantage is the potential cost of maintaining multiple small tables and increased number of joins.

More details here

You can query that dynamic table and fetch those results into a DataTable the traditional way.

After this you can seralize this into a json string. https://stackoverflow.com/a/12063095/145682 … shows you how.

These can then be easily consumed by js libraries like knockout.js, or, jquery, etc and rendered on the Web page.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật