I have reverse engineered a MySQL database in a C# desktop app. What I would like to do is extend one of the model classes, so that I can add methods to it to use locally in my application. I don't want to change any properties or anything just. Just get information and calculate things.
The problem is that when I inherit from one of the model classes I get an error about a new discriminator field being in the class but not the database.
Is there a way to do what I want to do?
Given that the model classes are partial, you can just declare your own partial classes to join them:
// Note - needs to be in the same namespace as the auto-generated declaration
public partial class Foo
{
// Add your own methods here, which can refer to the properties declared
// for the same type in the auto-generated code
}
The point of partial classes is that multiple files can contribute source to the same type.
You could try extension methods to accomplish this instead of inheriting and creating a new subtype.
You would "attach" the extension methods to the model class which you generated.
Related
Extension methods are a great way to extend the functionality of a type.
Are there any ways similar to this which can be used to extend properties of a class without inheriting a new class.
No extension properties do not exist.
You can't do it via properties without inheriting a new class. There are only extension methods, not extension properties (it may be added at a future date). If you don't want to alter the original class you should inherit from the original class and then add your properties to the derived class.
My edmx file generate some partial classes. I want extend functionality adding one property, but edmx file is in another assembly. In this case they are like two unrelated classes.
How can I solve this?
You can inherit another type from the entity type edmx has generated and add your property to the inherited one.
you can Create a new class in your assembly that will extend/inherit the partial class and add properties and methods as you like
I have a situation where I have 4-5 very similar classes which I'd like to re-factor to use an abstract base class.
The idea behind this would be allow methods which could be used by each class whilst adhering to DRY principles.
The problem I have is that these classes are Entity classes generted from EF4 and each time I try to alter my model it seems to break down.
What's the best/recommended, method to add a base class into my EF model and make the existing classes in the model inherit from this as a base? At the moment I've got no problem adding the base class, giving it an ID property as it seems to require, and then creating the inheritence, but EF then moans about
'Error 3024: Problem in mapping fragments starting at line 18563:Must specify mapping for all key properties (MyBaseType.ID) of the EntitySet MyBaseType.'
In this situation I don't want the base type to be persisted to the DB, purely used as a container for base methods common to all the classes which inherit from it. It feels like I'm missing something simple here but I can't for the life of me see it.
Any ideas on how to add in a base class in this way? Or should I just be adding the base class in code and bypassing the model somehow?
EDIT: As further information, take an example where there are say 3 types, MortageApplicationForm, BankAccountApplicationForm and CreditCardApplication form. They are currently stored in 3 different tables, with a set of different fields.
What I'm trying to do, is create a base class of say 'Form' which will have the common fields in it.
At a simple level, say that each table has a primary key ID field called, 'CreditCardFormID', 'BankAccountFormID' etc What I'd like to do it create a base 'Form' class with a property 'ID' which for the case of one table will map to 'CreditCardFormID' and another 'BankAccountFormID'.
I'm happy to do this mapping in partial classes (as I don't want to persist 'ID' to the DB) I simply want to use it in code, so I can write generic methods for things like LoadForm(int ID) without needing to write huge switches for each entity type, or specific methods for each entity type.
I managed to find a work around for this by rejigging things slightly. Firstly, I did not have the time available to rework the model (which I think would have been the best solution) too much of the system has already been developed with the existing structure to rip it all apart at this point.
The solution so far, has been to create a static helper class to contain business logic which is generic accross in my example, the 3 different account types.
This was coupled with an 'IAccount' interface, allowing the helper class to take an IAccount instance as a parameter (allowing the passing of any particular account type.) This interface contained all the common properties accross the 3-4 concrete classes. It was important to note that in order to create generic methods which I could call on all the classes I was not able to use any other properties specific to the class.
Within the helper methods, I needed to switch my concrete XYZEntities instance to a more generic 'ObjectContext' object and then use the methods such as 'AddObject' rather than 'AddBankAccountForm', 'AddCreditCardForm' etc explicitely.
This involved a tiny bit of GetType()'ing to ensure the object was passed to the correct ObjectSet, but seems to work as desired.
There are 3 patterns for this:
Table per Class Hierarchy. All concrete types in the inheritance heirarchy are stored in one table.
Table per Type. Each type in the inheritance is stored in it's own table.
Table per Concrete class. A table for each concrete class but no table for the abstract class.
In your case with the existing tables the Table per Concrete class looks like the best fit.
There is a good description of these options in this book
I've tried to do this in the designer, but I wasn't able to figure it out. Is it possible to persist nested classes using Entity Framework?
Note: I am just curious whether this is possible or not. I can't think, at this point, if there would ever be a reason to do this, but it might be nice to know how if it is possible.
Example:
public class NormalClass
{
public class NestedClass { }
}
Update:
Danny Varod had a good idea for how to accomplish this. When I have some spare time, I'm going to try it out, and I'll post the results on here, unless someone else gets to it first.
EF classes are declared partial, so you can add whatever you want to them.
Note that the inner-class's properties won't be persisted to the DB, if you want that, use a navigation property instead.
A nested class in .NET is basically the same as a class within another level of namespace (accept for the fact the inner class can access private parts of outer class, as Ladislav Mrnka pointed out - you could use internal instead of private to get around this), there is no change in behaviour (unlike in Java), so there is not much point in using nested classes.
You can define sub objects using complex properties or using a navigation properties, however, complex properties have limited capabilities (no navigation properties or keys in them) and neither are created as nested classes.
To force EF to use nested classes, you could try creating the classes yourself, then mapping them either with a Code-First approach or by cancelling the auto-creation of the class and writing them yourself (or changing the .tt file to created classes nested) and then editing the .emdx as an xml to map entity to a different class.
This question already has answers here:
Why/when should you use nested classes in .net? Or shouldn't you?
(14 answers)
Closed 9 years ago.
Would like to know when it is right to uses a nested classes in C#?
Do we have incidents in which the use of it is unjustified and therefore not correct?
If you can give examples for both situations
Thanks
I find it's convenient to use a nested class when you need to encapsulate a format of data that is primarily going to be used within the parent class. This is usually because the purpose or format of the data is so bespoke to the parent class that it's not really suitable for wider use within your solution.
Here's a simple basic introduction to nested classes.
Nested_Classes
C# doesn't have a way to write a using directive to target a class, so that the static members of the class can be accessed without writing the class name as a qualifier (compare with Java's import static, which does allow that).
So for users of your classes, it is a little more convenient if you make any public classes as direct members of a namespace, not nested within other public classes. That way they can pull them into the global namespace with a using directive.
For private classes, go nuts, preferably put them close to where they are used to enhance the readability of your code.
I am not sure if there is room in my world for nested classes. It simply blurs the design for me. If you need to hide the information inside a class, why not just store it in member variables?
Besides, testing becomes more cumbersome without the ability to inject a stub in the place of the class.
User of Nested class is depending upon the scenario like below.
1) Organizing code into real world situations where there is a special relationship between two objects.
2) Hiding a class within another class so that you do not want the inner class to be used from outside of the class it is created within.
Suppose you have 2 classes called A and B and class B is depending upon class A without class A you cannot use class B # that scenario you can use nested classes
As per my knowledge
DataRow class is nested class for DataTable
i.e you cannot create a DataRow Class untill u declare a object of DataTable class
I find two main resons:
Personalize a class' name without ruining it.
Example: Vercas.ExplorerView, where I personalize the name of my class without ruining the meaning.
Private classes.
Example: Vercas.ExplorerView.Item is used only inside Vercas.ExplorerView.