3

i'd like to create something like wrapper or mayby better word would be "Extension" for generated in EntityFramework model class...

I've got model USER, with password, username etc... and user is in relation many-to-many with some other objects... whatever...

I'd like to create something like this:

class ExtendedUser : USER {
 public void AddObject(Object o) {}
}

But i don't know, is it good idea... I don't know how to create constructor. I'd like do something like this.

User u = ...;
ExtendedUser eu = u as ExtendedUser;

Conceptual i'd like to fetch data from DB and put it into ExtendedUser instance, because this object will have methods to manipulate on this data...

How to do this?

2 Answers 2

7

I believe that the classes generated by the entity framework are partial classes, so you could create another partial class with the same name, within the same namespace, and you should see any extra methods that you add on the user class, e.g.:

partial class User
{
  //Generated code
}

partial class User
{
   public void MyMethod();
}

User u = new User();
u.MyMethod();
Sign up to request clarification or add additional context in comments.

1 Comment

This is very helpful info. Thanks. I did not know you could do it.
0

If you just want to extend methods, that's enough. However if you also want to add metadata to your model (like data annotations, etc.) this approach doesn't works.

In fact, you can only add methods to the auto generated class.

I answered a question about adding and preserving data annotations to auto generated entity classes, here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.