asp.net mvq regenerating autogenerated view code after database table update - asp.net-mvc

i have generated the views (Create, Edit, Index, Details) for a table using LINQ to SQL.
My question is:
Once the views are created in Visual Studio using "Add, View", they dont change when i update the database (using Server Explorer) and the LINQ to SQL code.
Is there anyway to "Refresh" the view code, or do i just have to delete the existing ones and create new ones?

I see what you mean and as far as I can tell there is no way other than to begin again.
Having said that though any changes you make to the LINQ to SQL classes I would imagine be fairly minor so coding them wouldn't really pose that much of an issue.
I wouldn't, personally, delete and start again when I add fields I'd simply code in the changes as generally speaking I've modified the auto generated views to reflect my styles and layout.
Using the AutoGenerator is great for doing a lot of the grunt work up front but after that you're kinda on your own I think.

It might be handy to post a few lines of code here but here are the things I would check;
Is the database being updated
When you return your view after a postback are you reloading the data?
Are you doing a JSON postback and not handling the callback event?
After the submit are you returning the FormViewModel with the new data in it?

Related

Entity Framework 7 and ASP MVC 5 - simple tasks

I am sorry if I am asking something that has an obvious answer, but I have spent and entire day searching for resources on the subject and I fail to find or understand how to do a few basic thing with EF7.
So, here is my question.
I have an ASP MVC 5 (VNEXT) website and I am using Entity Framework 7. I have an existing database, thus I am working database-first.
So far everything was fine. I installed everything required to get my DNX EF commands up and working; I scaffolded a dbContext and I got all my tables as classes and a dbContext class.
Everything fine, all well. I was happy and continuing with my work.
However, I got to a point where I wanted to make a property of one of the generated (table) classes Required, because I use jQuery unobtrusive validation.
I have the following resource as a reference: http://ef.readthedocs.org/en/latest/modeling/required-optional.html
My first wonder is, according to this source, in the FluentAPI the property has been marked as .IsRequired(). I believe, making it required here is a whole other thing that has nothing to do with unobtrusive validation. So, the next thing explained is simply - go to your class and add the Required data annotation.
This is all fine and well, and after adding it, it works as it should.
But I immediately wondered - well, I am modifying the generated classes, am I not going to lose those changes once I update the model?
Which leads me to my final problem - I searched for a long time, I even played with the help menu of DNX EF, but I am unable to find the right way to update the dbContext and generated models after I make changes to the database.
I believed this to be something quite trivial but to my surprise I am unable to find a resource explaining how to do the update.
Can you point me in the right direction, and tell me how to update EF generated models and context after I make changes in the database schema, and what is the best way to add annotations to the properties of the generated classes?
The general consensus is that you shouldn't use your database entities as input from users directly. Instead, use ViewModels, verify those against your validation rules, then map them to database transactions.
As asp.net MVC developer I use database first and updating database is a big head ache so I use Metadata approach and create ViewModels which helps allot.

Can a user create a Table in SQL Database through a Form?

I'm working on a new project that will let users create new party event through a simple form and I need that data stored as a new table for each new event. What I wanted to know is if its possible for user to create new table in my Database by submitting the form? I know that you can write data to columns, but I have no idea if you can actually write an actual table with columns.
I'm working with MVC 4 and this is sort of new to me and I'm not sure if such thing is possible so wanted to check to make sure before I move to alternate path.
Thanks
First off: STOP USING THAT DESIGN
Read this page before you go any further too: Database Normalization
the schema for your database should looks something like this:
users
id,name,etc...
parties
id,user_id,date,etc...
Using your way you will run into MASSIVE problems down the road
What I wanted to know is if its possible for user to create new table
in my Database by submitting the form?
Of course that it is possible. If you couldn't insert records in a database after suibmitting a form in ASP.NET MVC, then this framework would be useless garbage.
I know that you can write data to columns, but I have no idea if you
can actually write an actual table with columns.
You may take a look at plain ADO.NET or an ORM such as Entity Framework which both allow you to query a relational database. By the way you might consider reading some of the getting started tutorials on the http://asp.net/mvc site. Here's one example you might go through.
yes, you can write SQL DDL Sentences. "CREATE TABLE" is the concrete expresion you need to use in this case. It may takes different syntax according the database you are using

ASP.NET MVC Database Views

Quick question about database views. Am I right in assuming that I can create a database of view of various tables and connect them how I want etc and then when I do queries, add, edit delete etc then MVC will figure it all out for me without needing to do any complex SQL in the controller or repository?
Odd question but just wanted to make sure my assumption was valid. Cheers
Unfortunately, MVC will not figure it all out for you, you'll still need to write the SQL code (or use an ORM framework) to communicate with the database.
What MVC gives you with it's architecture is a clear separation of responsibilities:
Views are responsible for displaying data and should be as simple as possible (i.e. little to no logic in them)
Model(s) contain the business logic and rules
Controllers are responsible for passing data between the Model and the Views.
What you are looking for is Scaffolding. In .net MVC I can't think of any tools which do this for you directly against the database. They all require either as Russ said an ORM i.e. Linq To SQL or Entity Framework (EF).
http://msdn.microsoft.com/en-us/library/cc488540.aspx
The closest you could get would be to use Database First model generation and then put the necessary MVC templates/views/code on top.
A database view is read-only so you will not be able to perform write operations on the view. You can however create a model from a view and display your data as defined from the view. If you are using an ORM solution such as ADO.NET Entities you can instantiate an object and add the child objects to it and be able to save the final result in a single transaction as well.

How to add a table to the EF4 Context dynamically in code - No Code First

We run a series of reports every 6 months and store the results to tables that can be queried/viewed at any time in the future. Depending on the cycle either two or four tables will be added. They have a standard naming convention of yyyy_mmm_Table_x.
Our website is built using ASP.Net MVC2 and the database is modeled using EF4 using the standard model designer, not Code First.
I would like to be able to dynamically add the report tables to the EF4 context at runtime. I don't want to have to manually add them to the model using the designer, otherwise every reporting cycle we have to update and recompile the model just because we added the extra reports. That would be a maintenance headache when nothing else has changed.
I can get a list of the available tables simply by querying sysobjects. If I could get this list and add the tables to the context when the site started up then I could use something like the Dynamic LINQ library to query against them depending on which table the user selected from a dropdown.
I can't use EF4's Code First out of the box because that would force me to create concrete classes for the tables and that would just be the same maintenance headache. I suspect I could use the same strategies the Code First framework uses to dynamically update the context, but I haven't looked at this library at all and I'm hoping someone familiar with it can point me in the right direction.
Otherwise I think I would have to drop back to ADO.Net to handle this area. That may be the best and simple way so I guess I'm looking for comments. I'm not a zealot so I don't need everything to be in LINQ and EF4. :) But it would seem to be a little cleaner and consistent, especially if it allows me to make use of Dynamic LINQ. But sometimes the old way is just simpler.
So, if you have any suggestions or comments I would love to hear them.
Thanks!
Even with common EF you still need new data type for each table because when you map the table you need new ObjectSet of new entity type to be able to run queries. As I know it is not possible to map two tables to the same entity even if table structure is absolutely same.
All runtime mapping is stored in MetadataWorkspace prepared by EntityConnection. So if you want to play with it you can start there but public interfaces of these classes don't look promising.
I guess you want to run Linq-to-entities on these tables so using Stored procedure returning data from correct table based on data parameter is probably not an option.
You should use common ADO.NET for this.

asp.net mvc LINQ TO SQL handling database events (alerts/feed)

Recently I did one web site (www.ramtajogi.com) using asp.net mvc, linq to sql .
I have used repository pattern to get data from the database. Now everything is working fine.
But the new requirement is to record all the events (on poetry book created, on poem added or when a new comment is posted to any poem. What is the best way to implement it.
Should I change my existing classes and do or is there any better way to do it.
Regards
Parminder
I think what you are looking for is a way to log across different actions:
Try this: http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filters.aspx
Maybe you want to go the "SQL Trigger" road? I know it's a much debated subject, but it allows you to log without changing any of your existing code.
I'm not sure about its impact, but if the triggers only do some inserts into a log table, I guess it should be quite low.
Thanks everyone,
Actually I created another table for alerts and added one record for every event.
Regards
Parminder

Resources