Getting Context errors when creating a CRUD application - c#

I'm trying to create a crud web app. I'm getting errors about the name space, which I can't figure out (sure it's something simple!) What does the type or namespace could not be found mean? It's only on pages related to the database. Or is there a way to link the sqlite database without a migration folder - ProjectContext file?

This class looks like it exists but in a different namespace to the one the controller is in. You will either need to provide a full refeerence to it's location:
Place1.Place2.ProjectContext
or a using statment at the top.
using Place1.Place2;
Visual studio has a shortcut to help you track down classes like this, right click on the problem class and click "Quick actions and Refactoring..." and you may be presented with an option to either insert a 'using' statement at the top or use a full reference.
Quick actions and Refactoring

Related

Can you call a class in a visual studio website project from a class library?

I'm creating a new VS2015 web application, but there's one piece that requires some reporting that already exists in another system.
The other system is a VS2013 solution that has a website (not web application) as it's main project, along with a number of class libraries. But the website directly contains a bunch of classes that do reporting and other things, and I would rather use those than recreate all the (very complicated) code.
Is there any way to reference the classes in the website from another project or class library? It's a rather large old application I'm maintaining, and I'd rather not try extracting all that functionality into it's own class library if I don't have to.
If those classes were already in a separate class library, I could reference them easy enough, but unfortunately they are right in the website, and I can't find any information about being able to link to it (presumably because you can't).
Here's a sample structure:
MySolution
MyNewClassLibrary
MyClass
{
MyReportFunction()
{
var x = new ReportClass(); // From website project
x.CreateReportFunction();
}
}
OldSolution
WebsiteProject
ReportClass
{
CreateReportFunction()
{
// All the code I'd like to access
]
}
Is this possible? Or do I have a lot of rewriting to do? Or would looking into converting the website into a web application be a better idea?
You can add those existing classes as linked classes into your solution.
To add an existing item to a project
In Solution Explorer, select a target project.
On the Project menu, select Add Existing Item.
In the Add Existing Item dialog box, locate and select the project item you want to add.
From the Open button drop-down list, select Add As Link.
You can also read more details here.
In case anybody else is looking for something like this, the answer is no, it's not possible.
I ended up pulling all the functionality out into a new class library project, which required massive testing to make sure I got all the little bits and pieces right.
Moral of the story - build your software properly the first time, and pull functionality out into reusable objects. Don't do procedural programming in an object-oriented language.

Entity Framework Error 3004 while trying to add a field into a table

this question is about adding a new field called "Position" of type integer into an existing table via the Entity Framework designer. What I expect to happen (correct me if I am wrong) is add the field, select to update the database via the model and get the new field created in my database. Instead of this, I get an "Error 3004 Problem in mapping fragments at line xxx". My environment is using Visual Studio 2015 with EF5, and the database is hosted on Oracle MySQL. The project I am working on is a website which was given to me to maintain and upgrade further.
Here are my steps:
1. Discover the table in the model browser, click Show in Diagram.
2. Inside the model I right-click on the table -> Add New -> New Scalar Property and type the new field "Position" inside the table.
3. Click save icon to save the model
4. Then I get an Error 3004:
5. I realise that in the mapping details my new field isn't showing and I can't type in there to add it manually.
6. I can see the new field has been added to the syncitem class in fact I want this to be an integer so I go ahead and change it by hand.
Tried to click save again and re-compile but I still get the same error. I need to say at this point that I have spent an entire day searching around on the web about various error codes I got other than just 3004 but here is where I stopped and turned to SO for guidance. I know there are other threads here about Error 3004 and I did read quite a few them. One of the pieces of advice I found was to change the DDL generation template to one that supports MySQL since Visual Studio will default to SQL (this is done by clicking on the white space inside the designer and then clicking the Properties tab):
I tried that as well but nothing changed, the error persists... I also tried deleting the entire table in the designer and updating from the database, the table re-appeared but with a torrent of "Error 11007" lines, plus the table's associations are not wired correctly. Another thing I did was to open the .edmx file in Notepad++ and see if I can add my new field in there, but it just felt wrong doing it like that so I quit.
Am I approaching this in the wrong way? All I want to do is add a new field! I have come to a stop, my experience with EF is not great, I felt lots of frustration with this database's particular setup, quite frankly I dare say that I think it is somewhat broken. It's like dealing with a brand new religion whenever I have to touch it!
In the long run I want to upgrade to EF6 and get rid of the designer but that's another story.
Any advice on my problem will be greatly appreciated.
Thanks.
Eventually I had to tamper with the XML file of the model (the .edmx file) to manually add in my new field and set it to integer, as there was nothing I could do to satisfy Visual Studio. The web project I am working on looks rather complicated with loads of sub-projects in it like "DataAccess", "EntityModel", "ManagerLayer", my web folders and a host of other directories. I think my EF needs a complete re-do, my guess is that it started with a very old EF version and is shuffling its feet right now in terms of maintainance.

how to add two xaml page with same name and separate code file behind in uwp

Hi I want to have two different layouts, one for mobile other for pc and for this I want to add two xaml page with same name MainPage.xaml and code file behind MainPage.xaml.cs. One page added into DeviceFamily-Mobile folder and other in main area of project.
Now when I run the app it gives this error:
"Type 'MainPage' already defines a member called '.ctor' with the same parameter types"
To overcome this problem I add parameter in constructor of mainpage for desktop. Now project runs successfully and both page and code works good but I just noticed that parameterized constructor of desktop page not being called...
Now my summarized question is how can i add two page with same name and different code file behind for specific device family.
I have search a lot but did not get answer of my question.
Please suggest me any solution of my problem any tip.
Thanks
Note: One more thing, I don't want different xaml page with same one code file.
i) To create a page which targets different devices the XAML file is named using a specific syntax based on MRT. We have tooling support for this in Visual Studio:
First, create a new folder in your project by right clicking on the project->Add New Item->New Folder. This folder needs to be named to target a particular device family – in this case we will use Mobile (which includes Phone) using ‘DeviceFamily-Mobile’.The tailored XAML view is then created in Visual Studio using the ‘XAML view’ template. Select the folder you added, then right click->Add New Item->XAML View. Make sure you edit the name to be MainPage.xaml (by default it will be named MainPage1.xaml). You need to use the same name so that the code behind will be shared an the page correctly loaded.
Both views share the same code behind (MainPage.xaml.cs in the above example), and creating an event handler in one can also be used in the other view. When the app is run, Universal Windows will pick the correct view based on the device family that the app is being run on.
ii) If you want to special case code for a particular view you can test for the devicefamily as follows
if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile")
You can not. You must create 2 difference Page to have difference code behind.
I would suggest:
Refactor your code behind using dependency injection and interface so it adaptive to device family
The Pages in UWP are partial witch means you can create a new cs and just write public partial class MainPage{} and continue the class from there. You could then use conditional compilation.
As I searched a bit I couldn't find any conditional compiling in UWP but only for Universal(Second link). But the first link might help.
Three ways to set specific DeviceFamily XAML Views in UWP
Conditional Compilation in Universal Apps

Possible to Add ServiceReference (ASMX) to Visual Studio Template (VSIX)?

When I add a Service Reference ("MyWebService.asmx") to my template, I have two issues.
The References.svcmap always has things like: Silverlight_Application_Template.MyWebService.ZrrayOfXElement.datasource. I don't think I really want the template name in the file name. Instead, I would like for them to get renamed to something more like: "$SafeProjectName$.MyWebService.ZrrayOfXElement.datasource" where "SafeProjectName" is what gets replaced by the name the user enters for the project when they create the template.
I get a PathTooLongException when I run the VSIX, because of the file listed above.
Is there a way to get this to work? I could just tell the users to add a ServiceReference immediately after they create a project using the template, but eliminating an extra step is always nice.
Thank you for reading and for any/all help/tips/suggestions. Have a great Wednesday!

ASP.net c#, how do I know what to use?

I keep coming accross code samples online for ASP.net c#, however they never seem to list which namespaces they include, for example:
using System.Data.SqlClient;
etc etc
Am I missing something obvious or should I be expected to know exactly what namespaces each code example requires?
When I'm in that situation, typically I search for the class on MSDN. The documentation will tell you which namespaces contain the class.
If they don't include them, you can follow this list in order:
Find that they are in one of the namespaces listed in the "blank code file" template , or
In Visual Studio You can click the missing type and press shift+F10 or Ctrl+. To get the option to automatically add the using statement (if the assembly is referenced)
With Resharper, Select the type and hit alt+enter for Resharper to find the namespace for you, and add it to the usings (possibly even reference the assembly as well)
Go to MSDN and search the name.
Go to Google and search the name (honestly, I normally do this before hitting MSDN anyway)
Compain to the article author
If code samples use the assemblies that a project references by default, then you can hover on the class name and click shift+F10 which will add the using statement automatically. If the class is not in any of the referenced assemblies then you are out of luck and need to know in what assembly does the class resides.
A quick google search can help, and in time you will memorize the namespaces... Of course its best if samples included the namespace and reference info, but mostly they do not.
If you are viewing code in Visual studio, just hover mouse over class or object you want and you will get tool tip about it if assemly of that class is present or you can google for particular class.For example if you want to know more about 'DataTable'class, just google it and you will come to know that its part of Syste.Data namespace.
I'm with the OP on this one. Having to just magically "know" what namespaces are required seems supremely silly.
I spent some time before C# as a Java Developer, and the NetBeans IDE will resolve these for you automatically. Ctrl-Shift-I, and it will insert all the packages (ie, namespaces) you need. If more than one package defines the class you are resolving, a window pops up and lets you choose which one you want.
For as fine a product as VS is, I am incredulous that this feature is not included.

Categories