I have an ASP.NET project. The namespace is MyNameSpace. Among the cs files in this project is TestClass.cs:
namespace MyNameSpace
{
public class TestClass
{
public string name { get; set; }
}
}
There is another class, Products. In the Product.cs class:
namespace MyNameSpace
{
public class Product
{
public string ProductID { get; set; }
public string Name { get; set; }
public TestClass testClass; <==Doesn't work.
}
}
Why doesn't it work? I've cleaned the solution, rebuilt, etc. There's no Intellisense for TestClass. I'm sure this is something stupid I'm doing but what?
Right click the TestClass.cs file in your Solution Explorer. Make sure the Build Action is set to Compile. You should then get Intellisense on that class and your Product class should be able to see TestClass.
First of all, please don't post questions that ask why "it doesn't work." That's not how programmers think. What happens exactly?
Second, there is no period after the namespace name.
Note: It's hard to tell if the periods are really in the code because this Aravol character has been editing the crap out of the question and it appears he removed one of the periods.
Related
I have a problem using a namespaced component which works in some places but not others:
Pages/Transactions/List.razor
<Test #ref="Test1"></Test>
Pages/Transactions/List.razor.cs:
using Accounting.Web.Components.Test;
namespace Accounting.Web.Pages.Transactions
{
public partial class List
{
private Test Test1 { get; set; } = default!;
}
}
The above reference to Test works as expected.
But then I want to to use the Test component in another component, so I do the following:
Components/Transactions/TRansactionRules/TRansactionRules.razor.cs
using Accounting.Web.Components.Test:
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!; // error
}
}
But using it in the component above produces the following error:
"Test" is a namespace but used as a type
At which point I have to replace the line with:
public Accounting.Web.Components.Test.Test Test1 { get; set; } = default!;
while elsewhere I can refer to it as Accounting.Web.Components.Test
It seems the folder / file structure has something to with it:
+Pages
+Transactions
-List.razor
-List.razor.cs
+Components
+Test
-Test.razor
-Test.razor.cs
+Transactions
+TransactionRules
-TransactionRules.razor
-TransactionRules.razor.cs
It seems when I try to use the Test component in a another component which is nested in a extra subfolder, reference to it fails, otherwise it will work.
I wish to refer to it as Accounting.Web.Components.Test rather than Test.Test regardless of where it is used. Am I doing something wrong, if so what?
I belive the error you are getting is because you are 'using' a namespace which also contains the full name of he type you are trying to use. I think removing .Test from the using directive should fix the issue.
using Accounting.Web.Components
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!; // error
}
}
(optional) You can move below code inside of _Imports.razor file in blazor solution, and outside of your component code. This way it will make it available everywhere into your blazor solution and not just one component file.
using Accounting.Web.Components
After accidentally hitting rename of my skv_match class to skv_player class I have issue with my ReadMatches method. The Visual studio keeps telling me, there is no definition of methods in class skv_player when I use class skv_match instead (after I renamed the class back to skv_match).
I am desperate and don't know if I am doing something wrong or Visual studio for Mac is. Does anybody know how to solve this or did I miss something in the code?
I tried to restart the app and laptop, rebuild and clean project. I also tried deleting the figuring classes, creating them again and pasting the original content in them.
public string ReadMatches()
{
var matches = _context.skv_match.AsNoTracking();
StringBuilder sb = new StringBuilder();
if (matches == null)
{
return "No matches found";
}
else
{
foreach (var skv_match in matches)
{
sb.Append($"Match id: {skv_match.id }\r\n");
sb.Append($"Match results: {skv_match.home_team}");
sb.Append($"{skv_match.home_score }:");
sb.Append($"{skv_match.visitor_score } ");
sb.Append($" {skv_match.visitor_team }\r\n");
}
}
return sb.ToString();
}
public class skv_match
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int id { get; set; }
public string home_team { get; set; }
public string visitor_team { get; set; }
public int home_score { get; set; }
public int visitor_score { get; set; }
}
I get error: "'skv_player' does not contain a definition for 'home_team' and no accessible extension method 'home_team' accepting a first argument of type 'skv_player' could be found (are you missing a using directive or an assembly reference?)" and same for other methods
I expect the app to just take this without any errors, yet I get error that class I am not referencing misses methods. Before I accidentally hit rename the class everything worked just fine.
Ok my apologies to everyone who took time trying to help me. There was issue in Entity framework DbContext. I don't know if I was just tiredly stupid, or mentioned missclick changed it.
For anyone trying to solve this issue, try Right click on the declaration and "Go to declaration". It will point you to the part where you define it.
To be specific, I clicked on part skv_match at var matches = _context.skv_match.AsNoTracking();
I just joined a new company and my manager just joined too, and he wants to change the way we program. basically do what he does. I'm wondering what's the difference, pros, cons, limitation and problems if there'll be any..here's the sample
namespace Models //this is the model part of from edmx
{
using System;
using System.Collections.Generic;
public partial class MyModelClass
{
public int ID { get; set; }
public Nullable<System.DateTime> PostDate { get; set; }
public string MyContent { get; set; }
}
}
this is the metadata:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Models
{
public class MyModelMetaData
{
//he wants all data annotation here
public int ID { get; set; }
public Nullable<System.DateTime> PostDate { get; set; }
public string MyContent { get; set; }
}
}
this is the partial:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Models
{
[MetadataType(typeof(MyModelMetaData))]
public partial class MyModelClass or MyModelClassPartial
{
//He wants the programming algorithm to go here
}
}
Please enlightened me. and he wants to create different metadata and partial classes per model class..way too many files involved.
thank you..i need an answer as to why..if you think his method is good..I will do this..but if you think this will cause problem in the future and more coding will be involve..i need to know
The first class you show, the entity classes, are generated from the database every time you save the EDMX (or when you execute the T4 Template).
This causes the file containing public partial class MyClass under the EDMX to be regenerated. So you cannot alter it, because the next time someone refreshes a table or adds one, your changes are gone.
That's why entity classes are generated as a partial: so you can create another partial to the same class to do your modifications in.
However, if you want to annotate your entity's properties with metadata, you cannot redefine the same property in the other partial class: the same name can only be used by one member of a type. So you can't do this:
// Entity class
public partial class FooEntity
{
public string Name { get; set;}
}
// User-created partial class
public partial class FooEntity
{
[Required]
public string Name { get; set;}
}
Because that code expresses you want two properties named Name in the FooEntity class, which is not valid.
So you'll have to come up with another way to add metadata to the type. Enter the [MetadataType] attribute. This works by creating a new class with the same properties as the class to be annotated. Here, using reflection, the metadata is resolved based on member name.
So when you create a metadata class for the above annotation:
public class FooEntityMetadata
{
[Required]
public string Name { get; set;}
}
You can apply it to the user-created partial:
// User-created partial class
[MetadataType(typeof(FooEntityMetadata))]
public partial class FooEntity
{
}
And also, in the latter partial, you can add members that add functionality to the entity model. New ([NotMapped]) properties and new methods for example.
I think the one use could be to not pollute the main class.
For example if you have a lot of attribute for validation (using dataannotation) and you don't want to have them in the main class you could use the MetadataTypeAttribute for that.
Another use could be if your class is auto-generated and you need to add some decoration (more attributes) to your properties without changing the autogenerated code.
When I create the Scaffold and add the Model class then I am getting these error "There was an error running the selected generator . try rebuilding the project"
I have three Model class :
1.Department.CS
2.Designation.cs
3.CompanyDBContext.cs
Database : I have two table in database, 1. Department(deptID,deptName,Description)
2. Designation(desgtID,desgName,description)
Objective :- I want to create one view page for these scenario. Like this
Insert Name of Form (TextBox) + Department Name (Dropdown list box) + Designation Name (Dropdown list box)
1.Department.CS
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
}
2.Designation.cs
namespace mvcAppraisalSystem.Models
{
public class Designation
{
[Key]
public int desgID { get; set; }
public string desgName { get; set; }
public string description { get; set; }
}
}
3.CompanyDBContext.cs
namespace mvcAppraisalSystem.Models
{
public class CompanyDBContext : DbContext
{
public DbSet<CompanyDBContext> Departments { get; set; }
public DbSet<CompanyDBContext> Designations { get; set; }
}
}
This is the problem with the connection string you can change the connection string with your DbContext name like bellow on the file web.config:
<connectionStrings>
<add name="MovieDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MVCmove-20140616082808.mdf;Initial Catalog=aspnet-MVCmove-20140616082808;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
For the class:
using System;
using System.Data.Entity;
namespace MVCmove.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Default1Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
I got this error attempting to build my first Scaffolded item on a newly created project using database first EF.
It's important to Build the Project after adding a new Entity Data Model. Apparently, it's necessary to initialize/configure aspects of the project.
I had read about this previously in a database first tutorial, but had forgotten it. Eventually, I remembered it after digging through related SO issues, including this one.
I built the project as they suggested in the error: "Try rebuilding the project".
And then I tried it once again. And the error was gone.
The creation of new project did not work in my case me.
Running a build did.
Fixed mine in VS 2019 by closing and reopening Visual studio.
Hope this helps someone.
I tried some of the suggestions in SO but in my case, it was something else. I forgot that I had created a partial class where I keep my Data Annotations which had a property that I removed from the model/database. So, after I updated the model from the database, project compiled fine I didn't notice any issue. But, later on when I tried to create a controller with scaffolding, EF started throwing "There was an error running the selected generator. try rebuilding the project". I finally able to resolve the issue after I remembered and removed the property from the partial class.
I renamed Model, then tried to add controller again, then I renamed Model to first name. After that creating of the controller was successful.
I was getting the same error above on VS2017 also.
My Solution was to unload my project and then edit the csproj file and look for the reference to System.Web.MVC and deleted it, then saved and reloaded the project. I then went to manage NuGet packages and you can then Reinstalled Microsoft.AspNet.Mvc package.
Cleaned and rebuilt the solution and tried to add the controller again and it worked!!
No idea if anyone else has had this issue since but restarting Visual Studio removed the error for me in VS2017.
This Solved My Problem
If you are using Visual Studio, rebuild the project as suggested in the error window. To rebuild, do the following:
Right-click on your project name in the Solution Explorer.
Click the Rebuild option (which is on the second number of the dropdown in Visual Studio 2017)
If you don't know how to open Solution Explorer, do:
Select View from the top menu, click on the option: Solution Explorer.
I am used to VB and is kind of new to C# syntax. But from what I read everywhere the syntax seems to be kind of identical for Partial Classes.
I am trying to make a partial class to a generated partial class generated with Entity Framework.
It seems like my class can't reach the other one.
The error:
'XmasMVC.CmsSystemPagePart' does not contain a definition for
'DefaultLanguageCode' and no extension method 'DefaultLanguageCode'
accepting a first argument of type 'XmasMVC.CmsSystemPagePart' could
be found (are you missing a using directive or an assembly reference?)
I can compile but then I get errors while running. Seems like my class overwrites the generated one.
This is the genereated class from EF.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace XmasMVC
{
using System;
public partial class CmsSystemPagePart
{
public string DefaultLanguageCode { get; set; }
public string PagePartName { get; set; }
public string Description { get; set; }
public string DefaultLangValue { get; set; }
public string LanguageCode { get; set; }
public string Value { get; set; }
}
}
This is my class.
namespace XmasMVC
{
using System;
public partial class CmsSystemPagePart
{
public string GetValue()
{
if (this.Value == null)
return this.DefaultLangValue;
else return this.Value;
}
public string GetLanguageCode()
{
if (this.Value == null)
return this.DefaultLanguageCode;
else return this.LanguageCode;
}
}
}
Found this "Warning":
"The type 'XmasMVC.CmsSystemPagePart' >in 'C:\VSS\Comdigit\Xmas\XmasMVC\App_Code\CmsSystemPagePart.cs' conflicts with the imported >type 'XmasMVC.CmsSystemPagePart' in 'C:\VSS\Comdigit\Xmas\XmasMVC\'. Using the type defined >in 'C:\VSS\Comdigit\Xmas\XmasMVC\App_Code\CmsSystemPagePart.cs"
This is how the Objects Browser looks like. Strange for me!
This link here explains that
In general, ASP.NET creates an assembly for each application directory (such as App_Code) and one for the main directory.
For your case, although the partial classes are in the same project and the same namespace they are not being compiled into the same assembly.
Try moving the source files out of App_Code and make sure their Build action is Compiled in the properties window.