Problem with namespace from a nested folder - c#

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

Related

Using a Database Context in multiple projects within the same solution

I'm currently working on a program that is being used to generate PDF's and documents. There are two different use cases, one being an automated process and the second being a manual process where data can be edited via a front-end app.
The solution has 2 Projects in it, the first for the automated part, and the second for the manual part.
However, since the two processes make use of the same data and templates, I've split the solution into two parts, this will allow me to set it up in a way in which I only need to maintain models/templates once.
My database context looks like this:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RefundTracker.Models
{
public class DatabaseContext : DbContext
{
public DatabaseContext()
:base("Prod")
{
}
public DbSet<Referral> Referrals { set; get; }
public DbSet<ReferralAppointment> ReferralAppointments { set; get; }
public DbSet<ReferralPayment> ReferralPayments { set; get; }
public DbSet<BankDetails> BankDetails { set; get; }
public DbSet<ReferralAppointment_History> ReferralAppointment_History { set; get; }
public DbSet<ReferralPayment_History> ReferralPayment_History { set ; get; }
public DbSet<IsInUse> IsInUse { set; get; }
}
}
In terms of projects, I have a project called "RefundTracker" and another called "MailMergeTPA".
The context provided above, together with all of the models, are located in the "RefundTracker" project.
I would like to make use of these models and context in the "MailMargeTPA" project as well.
I referenced the "RefundTracker" in "MailMergeTPA" project, however, no results when using the context here. (When I access a function that get a list of names for instance, I get the full list in "RefundTracker", however, I get no results when I use the same function in "MailMergeTPA".
Code Example:
public BankDetails GetBankDetails(Referral record)
{
string bName = record.bankName.Trim();
try
{
BankDetails bankDetails= new BankDetails();
List<BankDetails> bankDetails = new List<BankDetails>();
using (DatabaseContext db = new DatabaseContext())
{
bankDetails = db.BankDetails.SingleOrDefault(a => a.BankName == bName);
}
return bankDetails;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
I would like to make use of this exact function in both projects.
Could you kindly help me with some advice? (Please go easy on me in the comments, I'm still fairly new to EF)
I've tried referencing the project, no result.
I've read up on interfaces, however, I'm unsure as to how I would incorporate this.

C# EF Required Attribute not recognized

I am creating an app that uses a database (SQLite). I am using entity framework and ADO.NET to interact with it.
I have a seprate Models project in my app that contains all of my Database models.
Now I want to mark some of my class properties as required to reflect the "NOT NULL" option in my database. But if I add the [Required] Attribute from DataAnnotations namespace I get a compiler error saying it cannot be resolved.
Here is how my class looks like :
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ReflowModels
{
public class Tag
{
public Tag()
{
this.Options = new HashSet<Option>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
public ICollection<Option> Options { get; set; }
}
}
I have also added a reference to EntityFramework.dll in my project.
you need to add this to your using block
using System.ComponentModel.DataAnnotations.Schema;
you need to add this to your using block
using System.ComponentModel.DataAnnotations;
In case it still doesn't work, maybe you should add it to your References

Enum cannot work when reference it cross multi visual studio solution

I have three solution A, B and C.
I have an enum in A like this:
using System.Runtime.Serialization;
namespace A.Entities
{
[DataContract]
public enum Status
{
[DataMember]
Active = 0,
[DataMember]
Inactive = 1,
}
}
I reference it in solution B like this:
using A.Entities;
namespace B.Entities
{
public class User
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public Status Status { get; set; }
}
}
In solution C, I use them like this:
using B.Entities;
using Status = A.Entities.Status;
namespace C.TestDatas
{
public class UserTestData
{
public static User CreateUser()
{
return new User
{
Status = Status.Active,
}
};
}
}
When I invoke User method, it throw exception:
Method not found: 'Void B.Entities.User.set_Status(A.Entities.Status)'.
Why? I hope someone can help me, thanks!
I can't reproduce the problem. Your code works fine on my computer (on .NET framework 4.5)
Could you make sure that you project is configured correctly:
Both A and B solutions have reference to System.Runtime.Serialization
C has reference to A and B. Make sure you don't link the dll from /Bin folder, but you have the project reference
Hit Clean and Rebuild buttons in Visual Studio.

Why am I missing using directive or assembly reference ASP.NET

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.

Partial Class to a generated class in EF bug?

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.

Categories