Partial Class to a generated class in EF bug? - c#

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.

Related

Problem with namespace from a nested folder

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

what 's the use of creating a metadata class and a partial class for the model class c#

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.

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.

Entity Framework DbContext code generation generates incorrect code

I'm using VS2010 with Entity Framework (file version is 4.4. product version is 5)
I have installed the EF5.x DbContext generator.
After creating my .edmx file, I right clicked on the empty space and added a new DbContext template, which generated the context.tt and .tt files.
However, in the .tt files, this is how the auto generated code looks like:
namespace DataObjects.EntityFramework.Models
{
using System;
using System.Collections.Generic;
public partial class SubSystem
{
public string SubSystemId { get; set; }
public string Description { get; set; }
public string Fmode { get; set; }
public Nullable<System.DateTime> LastBackup { get; set; }
}
}
The problem is that the using statements are inside the namespace, which gives rise to a compilation error.
Those compilation errors must be related to something else, because it's perfectly legal in C# to have using statements in the namespace.
Verify that you've added all of the correct references, such as EntityFramework.dll

Error Saving ViewState in Entity Framework

I am working in .NET Entity Framework 4.0
I am using viewstate to save an entity. And I have serialize that entity as well. But when I try to save data to viewstate, getting this error:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Error serializing value 'System.Collections.Generic.List`1[Pc.PrecisionCare2.ModelTypes.Medication]' of type 'System.Collections.Generic.List`1[[Pc.PrecisionCare2.ModelTypes.Medication, PrecisionCare2ModelTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].'
Maybe this will help anyone. I wanted to serialize a entity in the Viewstate myself and couldn't find a good solution (XMLSerialization, Byte Serializing, DataContract). What I found out is that I could "extend" the code generated classes (they are partial) and make it Serializable.
For example this is a .net code generated entity:
//------------------------------------------------------------------------------
// <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 OAuthServerBroker.EF
{
using System;
using System.Collections.Generic;
public partial class ResourceOwner
{
public ResourceOwner()
{
this.Grant = new HashSet<Grant>();
}
public System.Guid ResourceOwner_ID { get; set; }
public string ResourceOwner_Username { get; set; }
public virtual ICollection<Grant> Grant { get; set; }
}
}
When I create a new class file with the same class name and namespace I can make the Entity Serializable :).
using System;
namespace OAuthServerBroker.EF
{
[Serializable]
public partial class ResourceOwner
{
public EntityState State { get; set; } //can even put into new properties
}
}
Hope this might help anyone since it's an old post :(.

Categories