简体   繁体   中英

Why does moving classes into a new folder in Visual Studio break things?

I have an ASP.NET application with three areas. In the root of the application is a Models folder containing a hodgepodge of classes, including two DataContexts, some Repository classes, some ViewModel classes, some helper classes, and some business logic classes.

I'd like to create subfolders for each of these subcategories and move the associated files into the appropriate subfolder. However, I am noticing that this is breaking things. For example, if I move one of the DataContexts to a DataContext subfolder in the Models folder, I find that the code in the repositories can no longer see the DataContext type.

Which must mean that the Models folder has special meaning to the system. What am I missing here? Is there some way to "sensitize" the subfolders so that the other code can still see the implemented classes and namespaces? Or am I going about this the wrong way?

Probably a namespacing issue? The model folder has no special meaning.

The ASP.NET MVC team has said something like before:

" The only reason the model folder exists is because people kept asking us where to put our model classes " ;)

When you drag and drop your files and folder, it will move the the files, but the namespaces are not updated, thus your code will break. One way is to create new file/folder and copy your code - which is a bit tedious but right way. Another way is to use Resharper - Ctrl+Shift R, which lets you move the file and also updates the namespaces and your code will not break. I am finding a faster way to do the same without Resharper, and when I do I will update my answer. Hope this helps!

Taking my first steps in C# I came across this post... in VS 2019 if you refactor->move the class file you have options to move to another location

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM