问题
I have an ASP.NET MVC 4 project with a lot of models to be scaffolded in a custom way. I have customized the templates to create the controllers and views how I want them to be. This all works fine. Now I've written a PowerShell script to generate the controller and a couple of views at one time for each model that needs to be scaffolded. This also works fine.
Then I went looking for a PowerShell script to scaffold multiple models in one command, because now I have to manually run the script for each model that I have. First thing I found was to write a script with on each line the command to scaffold a model, which worked fine but was still too much work in my opinion. After a little deeper searching I found that this could be done with a script that has a list of the models to scaffold and a foreach loop that iterates over the list and then executes the scaffold-command for each item in that list. This was much better!
The only thing I'm asking myself now: is it possible to write some script that automatically detects all models in the Model folder, puts them in the list and then performs the loop. Reason why I'm asking this is because I don't have just a couple of models, but about 250. It's not that I don't feel like doing this manually, but what if some models are added, changed or removed...
So my question is: is there a way to 'dynamically' get all the models from the Model folder and put them in the list that will be iterated?
Update:
This is how my script "ScaffoldAll.ps1" looks now (not all 250 models are in the list):
$models = "Team", "Player"
foreach($model in $models)
{
Scaffold CustomController $model -Force
}
What I want to achieve (pseudo!):
$models = //All files in Model folder, like: Get-Files "\Models"
Is this possible?
回答1:
Abbas,
I've been using the MVCScaffolding template (with EF code-first) and the way that this works is that you create your domain model(s) and then scaffold ALL objects that have a domain model. This template will actually skip the scaffold for any pre-existing controllers/views that have been scaffolded. However, there is also an override command (-FORCE) which will then overwrite any/all pre-exisiting scaffolded code if you so wish.
here's the quick link:
http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/
回答2:
@jim: You didn't provide a direct answer (which I did not demand) but you sure got me on the way for which I thank you very much. I went looking into the templates and by studying these a bit, I found a solution to get the models from the Model folder. It might not be the perfect solution, but this works perfectly:
$modelFolder = Get-ProjectFolder "Models\"
foreach($file in $modelFolder)
{
$model = $file.Name.Replace(".cs", "")
Scaffold CustomController $model -Force
}
来源:https://stackoverflow.com/questions/9208471/automatically-detect-all-models-to-scaffold