ASP.Net MVC: Best Practices for dbml files - asp.net-mvc

This may just be a yes or no type of question but here goes anyway...
From all (well most) of the examples that I've seen for using mvc, it appears that method for creating the dbml file is just drop the entire schema of the database into it and let it autogenerate all of the linq to sql goodness. It appears that you just need one of these (as you can't have duplicate tables in separate dbml files in the same project) but for some reason it would seem like there's a better way to do this...especially when dealing with a large project that has a fair number of tables.
So is this the proper way to go about creating a dbml file to use in a mvc project, just drop the entire table structure into and go to town? If not, how do you do it?

If the schema was large, I think i would be relying fully on a SQLMetal script to generate my *.dbml and backing classes. This way you can just regenerate your entire data model whenever your database gets updated. Otherwise, if a table, view, etc, gets updated in the database you don't have to delete and then re drag-and-drop that table into your visual *.dbml file.
Actually, I am not expert with SQLMetal, but I think you can even use it it to generate everything you need for Linq-toSql and not even require/generate a *.dbml file.

I'm not sure yet - its a problem I'm still working on but I think that the answer is that should it be desirable to have multiple dbml files - effectively views of your data - then you want to host the dbml files in their own projects so that you can have the similar things in multiple namespaces and not have them conflict.
This being the case the next logical step is to put your dbml files/models into their own projects by default and to learn to work with them when set up that way. This will also aid reuse of a model of database where you have more than one application interacting with that database.
There are certainly issues with separating the thing out and also with having multiple dbml files in a a single project (in terms of ensuring that extensions to the classes are implemented conistently in all instances for example) but I've got a case where its not inappropriate.
Good question, answer probably tends towards being "just one" but not in every case...

Personally I prefer to create the classes/association in the .dbml and then generate the database from that.
Just add the following class to your project
partial class MyDataContext {
partial void OnCreated() {
if (!DatabaseExists())
CreateDatabase();
}
}

Related

Intellisense namespace discovery in Visual Studio not working after moving files to another project

I started with a single ASP.Net MVC project named Yogabandy2017 that had a folder called viewmodels. I stored all the viewmodels in it.
I moved all the files from that folder to another project called Yogabandy2017.Models where I stored all my models and I created a folder in it called Viewmodels where I put all the viewmodels, I also changed the namespacing in each of the viewmodels to match.
Then I did a large copy and 'replace all' in each view so that each view can now point to the correct folder like this
from this
to this, where I added the .Models to make the path correct
But intellisense still hasn't changed and picked up the correct path. I've tried to clean the solution, rebiuild it a couple of times. and still the path isn't connecting correctly. I still have the red underline and an exception being thrown when traversing the page saying it can't find the path.
Is there any way to get these paths to reset without going to each file and rewritting them manually? I have a few hundred and going through each one by one to make the change will take forever.
Any help would be appreciated...
Check your cases...
YogaBandy2017
vs.
Yogabandy2017

Saving a sql file after creating a Controller?

Visual Studio wants me to save a sql file dbo.Table.sql after I create a Controller for the model that represents the table in my database. The tutorials and documentation that I find online do not mention this at all.
http://www.w3schools.com/aspnet/mvc_models.asp
http://www.asp.net/mvc/overview/getting-started/database-first-development/generating-views
So it opens up a safe dialogue after I specified the Model and Context and press add on the Add Controller dialogue. The path starts in my Documents folder but I am not sure where to safe this, I would logically place this in App_Data.
So why does it ask me to safe the sql file?
And where do I need to store it?
And perhaps, why is this not mentioned in the documentation? I'm pretty sure I am doing it in the same manner. Create SQL Server -> Add tables -> Add Model -> Add MVC5 Controller with views using Entity Framework
The answer is simple, I had a typo in my Model class so it did not find the table, you can see it in the screenshot. If you ask me it would be better to throw a warning when adding a controller that does not have a correctly mapped table and give the option to create a local script file for it. On the other hand, now I know this it's probably obvious.

Where to store string constants in my ASP.NET MVC app?

Specifically, I have a phone number that is used in a bunch of views(and a couple actions), and I would like to centralize that somewhere in case it changes. Is web.config good enough? Is there a better place for this? Ideally, I wouldn't have to recompile if the value changed. Thanks!
Update: So far, I like the partial view the best, the main reason being no recompile, no adding it to viewmodels. Another option I've explored is Application_Start in global.asax and using the Application dictionary(although it sounds like the use of this dictionary is frowned upon in mvc). One thing to note is that I need to use this number in some actions also. Any more thoughts/opinions?
In no uncertain terms do you want a view accessing your web.config file.
If this is something which shows up in many places in your views and requires a static set variable, then make a partial view to display the number, and call the partial view multiple times.
The advantage is that you are keeping it in a re-usable area so that if you change it once, it changes everywhere, and also that you are not going to have any separation of concern violations by having your view make calls where it shouldn't have access. Updating the partial will also not require a re-compile.
/Shared/_PhoneNumberPartial.cshtml
<span>555-1234</span>
Used in a view
<div>Phone Number<br/>
#Html.Partial("_PhoneNumberPartial")
</div>
Resx Files, resource: http://msdn.microsoft.com/en-us/library/ekyft91f(v=vs.90).aspx
Config Files, resource: How to use .NET configuration files (app.config, settings.settings) to save and restore all application data?
Class Files, resource: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx
Settings Files, resource: http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
also if you would like to be able to update the settings without touching the database, it will be a great idea to save it in database or xml files. Then create an interface(web page) which communicates with that table and updates the settings. Create a repository which caches the settings every time app starts and invalidates the settings every time one gets updated.
For this case specifically, I would save it in class file. but the best solution depends on the project, later scale of it, and etc, etc.
For something like a phone number I'd put it in a data storage of some kind. If you don't need a full database consider something simple like a file based storage or even a simple *.json or *.xml file, preferably in the AppData folder. However if you do this you should be passing it to your view via a ViewModel and let your controller or some other method do the actual reading from your data storage. Views should not access these directly.
I find the web.config is best suited for configuration settings. It is a config file after all. It's not the best place to put content. Your phone number is content, and to me Content should be in a dynamic and structured location.
You can add the phone number to appSetting key/value in web config like below:
<appSettings>
<add key="PhoneNum" value="1234567890"/>
</appSettings>
Then you can use it
using System.Configuration;
string Phone = ConfigurationManager.AppSettings["PhoneNum"].ToString();

Asp.net MVC, adding edmx gives trusted zone warning

I have an ASP.net MVC 4 project I am working on and am trying to add an ADO.net EF model to it using Database First.
The creation of the model (.edmx file) seems to run successfully and I am left with the desired .edmx model file in the folder I specified. However, I don't appear to be able to see any of the files nested under this model. (From all my research, I should be able to expand the .edmx file and see *Context.vb, *Designer.vb, *.edmx.diagram and *.tt files underneath, but I cannot.)
It should look something like this in the msdn article about Database First (see Step 4):
Upon noticing this issue, I discovered a handful of warnings that appeared after creating the .edmx model, one of which is (I have obscured part of the full path):
The path 'P:\IT\...\DAL\EF.Utility.VB.ttinclude' must be either local to this computer or part of your trusted zone. If you have downloaded this template, you may need to 'Unblock' it using the properties page for the template file in File Explorer.
The project is stored on a network share on one of our servers so it can be included in our daily backups and Windows Shadow copy also. My understanding is that the above warning has appeared because of this and I need to set my machine to trust this location.
I have tried all possible variations I can think of of
this MSDN article
but to absolutely not success at all, the warning remains.
Either I am looking in the wrong place, or I have missed something.
Does anyone know what I can do to remove this warning and gain access to the objects nested below the .edmx model?
Perhaps this could be of some assistance. I've never really dealt with trust issues working across the domain. I lean towards pulling down and working with a local copy.

Resource(resx) Custom helper

I'm a first time poster long time listener and I would really be interested in reading about some of your localization architectures and, eventually, to get feedback on our approach (as follows).
I would like some advice on an approach we're thinking of using with resource files. We are using MVC 3.0 and have a website project and a resource project. In the resource project we have a structure which mimics the same structure as the website e.g. controller -> view -> file.
We reference the resx files in the views by importing the resource namespace on the top of the view/control e.g. <%# Import Namespace="MyAppResources.Resources.Website.Home" %> and then reference the resx value we need by using <%= Index.SomeText %> where index is the name of the resource file.
What we were thinking of doing and would love some advice is instead of using this approach is to divide the resource resx structure into website areas and use a helper e.g. LocalizationHelper.GetValue("Home", "SomeText") where "Home" is the name of the resource file and "SomeText" is a value in that resx file. The reason we would do this is not to have to keep compiling the resource project for every small copy change we make (as we may need a quick fix for our deployed environment) and also it will probably be the most commonly used helper in the website project so this would keep things short and consistent. The Localization helper would also store the values in a cached dictionary so if a value is used more than once it would retrieve it from the cache.
Does anyone know of a better approach or improvements we have not thought of?
I would recommend using a database to store the localized values instead of a RESX file.
Using a database would prevent you from needing to make any code/file deployments to update your application. Furthermore, you could build a GUI interface for modifying the localized values (which is a great feature for the site administrators/editors).

Resources