Context has null properties when entities in another project - c#

I'm using EF, and I generated two tt files. I left the context.tt in the ServerComponents project. I moved the entities .tt file to a Common project, to be shared by all projects in the solution. The problem is that all of the properties/entities within the context object are null at runtime, and I can't figure out why. It's like my context.tt doesn't know where the new entities are, but I don't know how to fix that. Does anyone know what I'm missing?
Here is the server project:
Here is the common project:
And this is the state of context at runtime:
I've tried too many things to list here. I'm hoping someone just knows this answer...

I think I got it. Through trial and error, making the CustodianEntities properties public worked (they were internal). This is in DataCustodianContext.context.cs. Now I just need to alter the tt file to make sure it keeps these public when generated again.
I considered deleting this question, but I'm hoping maybe this helps someone else.

Related

How do I set common values (abstract) for a C# solution in Visual Studio with multiple sub projects?

I have a solution with multiple projects each of which connects to the same DB and uses overlapping constant values that I would like to set somewhere instead of replicating manually. I have tried a variety of things online like making a custom class and linking projects to it, setting constants in a project config file (which doesn't exist like the guides claim), and so on. I've been unable to figure this out after more than an hour of searching and experimenting so if you have any ideas, let me know. The structure looks like this (the blue-underlined stuff are some of the projects in the list):
You can make another project under the solution to contain your class.
All the other projects can then reference that project, meaning the same functionality will be available in all the other projects without having to duplicate anything.
I will extend the previous correct answer with some more information.
Your solution structure is something to think very carefully as it is a combination of application design/architecture and leads to extensibillity, scalability and future maintainability.
Take for example the following article Common web application architectures.
You can see the Clean Architecture (AKA Hexagonal) which leads to specific projects withing a solution
You can see older designs where the DB access would go into a project called ..DAL
Simple projects can use the second one, more business rich ones the first or something in between.
Check this this article on shared code projects to see about net standard projects
So the above was helpful, but far more complicated than it needed to be. Apparently other answers I'd seen actually work, but it took reading a bunch of other pages to figure out the whole puzzle. The working steps are:
Create a class with public parameters for your constants
Place that class somewhere in your solution space. When I created it on the solution, it was placed in "Solution Items" in my tree (which is the root folder of the solution on the file system).
Right click each project and ADD>Existing Item and point to the class. The KEY (that was missing from most things I read) was that the "add" button" has a drop-down arrow that lets you change it to "Add as link"
In each project (after adding as link to the file), you can directly reference the values as NAMEOFCLASS.NAMEOFCONST but ONLY if you declared them as public const SOMETYPE SOMENAME. Without the const, it's not able to directly reference the value
Note that this fix is in the .sln file itself and needs to be part of the commit or it won't have any effect. It would be nice if you could use "include" or something to bring in a file a folder one level up, but here we are.

How to I find out what namespace a missing identifier belongs to?

When I compile my c# project in visual studio, I get the error ...
Error 1 The name 'FilterConfig' does not exist in the current context ...
I guess I need to add a 'using' statement or add a package or something. In general, whats the best ways to try and figure out what package/namespace missing things might belong to? E.g is there a way to search all common packages to find a member?
I've searched on msdn but cant seem to find it.....
http://social.msdn.microsoft.com/Search/en-US?query=filterconfig&emptyWatermark=true&searchButtonTooltip=Search%20MSDN&ac=4#refinementChanges=33,26,59&pageNumber=1&showMore=false
Update: This particular example is for MVC4, however I am interested for a general solution (or multiple solutions) as I work on console apps also.
I've often come across this problem when using incomplete tutorials I've found on the web. So the references may not be present at all. Usually they turn out to be for Microsoft.
General Troubleshooting Steps
Right-click on the identifier. In the context menu, you should see Resolveā€¦ ▹. In the sub menu you should see two lists which contain all of the namespaces in all the project's loaded references which contain a symbol with that name.
Selecting an item from the first list will add a using directive to the current file. Selecting an item from the second list will modify that use of the identifier to be globally qualified. Just select the option you want to use.
If the identifier cannot be found in any of the loaded references (either you are missing a reference, or it's a typo), then you won't see this list. In that case, you should make sure all the references are loaded correctly (there will typically be an exclamation mark next to it could not be loaded) and check the spelling of the identifier.
If all the references are correctly loaded, and your sure the identifier is spelled correctly, it's likely the symbol was renamed, or removed entirely from the project. Using the Renameā€¦ tool (also found by right-clicking on an identifier) can help to avoid this in the future. It's also possible the code snippet in question was taken out of a project where that symbol was defined and you need to include more code from that project to make the code functional. Finally, it may simply be that you're just missing a necessary reference. If this is the case you should investigate where this code came from and what libraries it uses, either by asking the original developer, or if it came from an online source, review the source to see if any more details are provided.
Regarding This Specific Issue
That's about it for general troubleshooting. However, for your specific issue, these steps may only get you so far, and you need to know more about where this exact class comes from in order to resolve it. Microsoft's MVC4 project template includes a number files in the App_Start folder. You typically start out with something like this (NOTE: not every MVC4 project template will contain the same files):
And these will typically be referenced in your Global.asax file like this:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
If this is where you're seeing the issue, it's likely that somehow the project was broken in some way. Either the FilterConfig class was renamed or removed entirely (as I stated above). It's also possible that the project started out life as an MVC3 template (which I believe didn't include the FilterConfig class) or perhaps a pre-release version of MVC4, and in migrating to full MVC4 template, this was left out. In any case, I would recommend you create a brand new MVC4 project and see what's different between the your project and the new one.
Now, if for any reason the new project doesn't compile and run like it should, then the template itself is probably damaged somehow. In this case, I'd recommend you uninstall MVC4 and re-install it from the official source.
Additionally to what p.s.w.g said (which you should normally do): I guess that you're using ASP.NET MVC or ASP.NET Web API and that the FilterConfig class is located in the *App_Start* folder. If so, you should remove the *App_Start* from the end of the namespace. After doing so you can call it without a problem in the Global.asax file.
Like said, normally you should use the solution provided by p.s.w.g, but for startup tasks this is the way Microsoft does it in its templates.

Can't refactor code: one or more predefined type(s) missing

I have a medium sized solution (~15 project) where I recently added some extra projects. And after that I need to rename the namespace for those added projects. And in past times it was just to change the namespace and I got the suggestion to refactor with preview. Now all I get is a message box saying:
"The refactoring operation cannot be performed because one or more predefined type(s) that are required for refactoring could not be found in your solution."
Does anyone have any idea?
The problem was that I had manually done changes in my csproj file. And some of those changes was bad. Unfortunately I just made a revert from my SVN repository, so I don't know exactly what the problem was... Sorry for that.

db4o How to rename class via configuration

I am using db4o in two seperate projects that share the same classes but not the same .dll . I am fixing this so that they share the same .dll but I need to rename the classes. According to the documentation you set up configuration and open the db with the renames and it updates everything. I have tried this but when I try to open the DB the project just hangs. Am I missing something here
config.Common.ObjectClass("DllName.Old, DllName")
.Rename("NewDll.New, NewDll");
var db = Db4oEmbedded.OpenFile(config, DBFile);
That looks correct to me and should work. It really should work when the class-names and assembly names are correct. The class is not a generic class, right?
You don't get any exception, right? Is db4o just hanging or actually doing some work?
Edit: I could reproduce a stack-overflow exception with renaming a class. Maybe you are running into a stack overflow exception as well, but you cancel it before it actually happens?
Anyway, I created a bug-entry in the db4o-bugtracker.
I was successful with the following answer:
https://stackoverflow.com/a/1138178/541420
Very buggy otherwise though :(

how to do a "Move type to another file to match its name" accross a complete solution

does anyone know of a way to split all classes in one solution into multiple files?
The point here is that I've inherited a project in which a few hundred files contain a thousand or so classes...
I'd like to be able to get to a 1 file per class approach..
Using resharper I can easily do this manually, but I'm guessing there must be a better way?
Kind regards
Frederik
You could try one of the ReSharper 5.0 nightly builds which allow you to do it across your whole solution. You can revert to ReSharper 4.5 (or whatever version you are using) afterwards.
GraemeF's answer is correct, but when you do that refactoring chances are you'll lose all source-control history for the existing classes. This might not be a problem for you (especially if the system you've inherited wasn't source-controlled!) but I've often found that line-annotated views of a class are very helpful for determining the intent behind a particular line.
ReSharper has the Ctrl-T shortcut to jump to a type name, and holding down Ctrl makes types clickable; that might be another way to solve your problem.

Categories

Resources