Can anyone tell me the specific steps to add custom methods to classes generated in from an .edmx file?
I have a ReturnedItem class, which has some simple and navigation properties. From everything I have read, I should just be able to add a ReturnedItem.cs file to the project, and define the same class as partial, similar to the following code where I implement the fullDetails() method.
However, I get an error that "trackingNumber does not exist in the current context" for every property I use. When I check the properties in the method with "this" it looks like ReturnedItem is a new object type that doesn't have the properties from the object defined in the model/edmx.
I'm missing something simple I'm sure, but no amount of research is pointing me towards my mistake.
using MyEntities;
namespace MyEntityDataModel
{
public partial class ReturnedItem
{
public string fullDetails() {
return "Tracking Number:" + trackingNumber;
}
}
}
Of course I figured it out seconds after posting. I knew it had something to do with the namespace, which you pointed out. It should be:
//using MyEntities; (don't need this)
namespace MyEntities
{
public partial class ReturnedItem
{
public string fullDetails() {
return "Tracking Number:" + trackingNumber;
}
}
}
As for the other question of posting my actual entity class, I didn't even realize how to do that. Now I know I should be looking at the Designer.cs file (which I understand is auto-generated) but that would have easily shown me what the proper namespace was.
Thanks for the help! You are ridiculously fast!
Related
So consider the case where I have a class ClassA inside of the project that is currently being generated into:
public class ClassA
{
public ClassA(int a)
{
A = a;
}
public int A { get; set; }
}
Let's say that I wanted to automatically create an extension method for ClassA, something like:
public static class ClassAExtensions
{
public static ClassA Double(this ClassA classA)
{
return new ClassA(classA.A * 2);
}
}
When trying to create this source code using the new source code generators, the compilation can't seem to find ClassA. I've tried adding the namespace of ClassA into the generated document and setting the namespace of the generated extension method class to the namespace directly to that of ClassA, but neither seem to be able to see it:
The type of namespace 'ClassA' does not exist in the namespace 'ClassANamespace' (are you missing an assembly reference?)
So the final questions are:
Is there some trick to making the code generation compiler be able to see my non-generated code?
Is this even possible right now?
Is there a workaround to get something like this to work?
Many of the samples provided declare the class being modified partial, but I don't particularly like this for what I'm trying to do.
I've also looked into adding an assembly reference, though my understanding was that the code being generated should be included and compiled alongside the existing code. Also, if this code is being compiled before my "production" code, then adding an assembly reference would not be possible and/or this would create a circular reference.
Files added in a source generator act like regular files from the perspective of the rest of the language rules so yes you can absolutely reference classes in the user's code as long as you're qualifying them correctly. It sounds like you have a bug; if there's still a specific problem you may want to try creating a project that contains both the input file and also the source generated output; you should see the same error and then can figure out what's up.
The issue I'm getting is "namespace is being used as a type" in a C# project. I get why it's happening and I understand what I have to do to fix it. But it makes little sense to me and so the question here is understanding the design of namespaces and classes in C# context.
I'm hoping this question is different enough from others because I'm not asking how to resolve the situation, but rather how to keep logical and relevant naming while dealing with the situation as I describe it.
I have followed MSDN conventions for naming solutions. So I have a solution called:
TesterStories.Voxam
I have two projects within that. One is TesterStories.Voxam.App and looks like this:
namespace TesterStories.Voxam.App
{
TesterStories.Voxam.Parser parser = new TesterStories.Voxam.Parser();
class Program
{
}
}
The problem is in that third line.
Presumably because I have another project called TesterStories.Voxam.Parser, which looks like this:
namespace TesterStories.Voxam.Parser
{
public class Parser
{
}
}
So is it saying that it doesn't like that I have "Parser" in my namespace for the project?
But according to MSDN guidelines, if the Parser is the feature, then that's what I should be calling the namespace: [Company].[Project].[Feature]. The class is Parser because, after all, that's what the class does.
So it seems like what I have to do is this:
namespace TesterStories.Voxam.Parser
{
public class Xyzzy
{
}
}
So here I changed my Parser class to Xyzzy class. Now back in my file that was causing the problem I can do this:
using TesterStories.Voxam.Parser;
namespace TesterStories.Voxam.App
{
class Program
{
Xyzzy parser = new Xyzzy();
}
}
So I get it -- but it means I have to change my class to be something it's not. I realize I could call it AppParser or ParserApp or whatever.
In this context, what do developers tend to do? Do you change your namespace or do you change your class? Even though, in my case, I'm describing the feature as per MSDN guidelines and correctly naming the class based on what it does.)
The namespaces and class names aren't the problem here.
In your original code, you need to designate what class from the TesterStories.Voxam.Parser namespace this 'parser' object should be.
Your original code:
TesterStories.Voxam.Parser parser = new TesterStories.Voxam.Parser();
What class/type in the TesterStories.Voxam.Parser namespace is this supposed to be?
You would need to do:
TesterStories.Voxam.Parser.*Parser* parser = new TesterStories.Voxam.Parser.*Parser*();
(* added for emphasis :) )
Or, like you did in your last code snippet, add a using statement:
using TesterStories.Voxam.Parser;
So that you can do:
Parser parser = new Parser();
Edit - More complete example (for the 'using' suggestion):
namespace TesterStories.Voxam.App
{
using TesterStories.Voxam.Parser;
class Program
{
Parser parser = new Parser();
}
}
namespace TesterStories.Voxam.Parser
{
public class Parser
{
}
}
I am trying to create a simple model for parsing a yaml file to my domain object using YamlDotNet. The caveat is, that I want the domain model to be readonly, so I'm attempting to solve this through inheritance and internal setters.
For some reason though, the library throws an exception stating:
Property 'HtmlTemplate' not found on type
'ConsoleApplication1.Repositories.YamlTemplateRepository+DeserializeableTemplate'.
I am using an alias, but even scratching that, and using a test class with the right property names does not set it right.
What am I doing wrong? Have I misunderstood how the library should be used?
The code that calls YamlDotNet looks like this:
deserializer.Deserialize<DeserializeableTemplate>(yamlContents);
and the class I'm deserializing looks like this:
private class DeserializeableTemplate : Template
{
[YamlMember(Alias = "HtmlTemplate")]
public string HtmlTemplateWrapper
{
get { return HtmlTemplate; }
set { HtmlTemplate = value; }
}
// A few more properties...
}
and the class I am inheriting:
public class Template
{
public string HtmlTemplate { get; internal set; }
// A few more properties...
}
(Small console test application with the same error can be found here)
Old question, but I had a similar issue, which was solved by changing the access modifier of the inherited property setter to protected. I'm guessing the internal modifier used here is playing tricks on the deserialization. This might be an unwanted solution for this problem regarding making the model truly readonly, but I wanted to share my solution for future troubleshooters.
I'm writing a dll in VisualStudio. This library contains two classes with the same name but in different namespaces.
public namespace XCharting {
public class Series { }
}
public namespace ObjectModel {
public class Series { }
}
Users of my library use first one. Other one is used in other my dll (In future it is possible that users will need the second class too).
The problem is when users write "Series" in their code. IntelliSense suggest to add
1)ObjectModel namespce
2)XCharting namespace
Is is tossible to change an order of suggestions or hide suggestion of adding ObjectModel namespce?
It seems it is impossible.
I have a code that I'm meant to compile into a .DLL file (it's for the game Call of Duty: Modern Warfare 3). However, it wont compile. Any ideas? Thanks!
using MapEdit;
using Addon;
using System;
namespace mp_terminal_cls
{
public class mp_terminal_cls : MapEdit
{
public mp_terminal_cls()
{
}
createfloor(new Vector(2263f,4406f,286f),new Vector(2958f,4147f,286f));
public override void OnMapChange()
{
base.OnMapChange();
}
}
}
I get 7 errors, the problem is the original code was exactly the same. I only added 2 new lines of code. Here is the errors:
Sorry I'm quite new to C# I only have about 2 months experience with VB.
1) Move the call to createfloor either into the constructor's body or OnMapChange's body (from your code, we can't tell which one you need):
public mp_terminal_cls()
{
createfloor(new Vector(2263f,4406f,286f),new Vector(2958f,4147f,286f));
}
or
public override void OnMapChange()
{
createfloor(new Vector(2263f,4406f,286f),new Vector(2958f,4147f,286f));
base.OnMapChange();
}
2) The base class MapEdit doesn't seem to have a OnMapChanged method.
As a side-note, your classes and namespaces should have distinct names, to avoid ambiguity issues.
There are essentially two errors
MapEdit is a namespace but is used as a type usually means that you
have a class called MapEdit inside a namespace called MapEdit.
Refer to it as MapEdit.MapEdit.
The rest are caused by the call to CreateFloor not being inside a
function. I assume that it should exist inside the constructor so move it inside