locked
Partial Classes of Entities RRS feed

  • Question

  • Hi

    I have a class library named "A" and another named "B". On B i have a LINQtoSQL file with the entities. On A i have the clases that it should be maped with the partial classes on "B".

    Example:

    Class Person

    Name
    LastName

    My question is? It's possible having a partial class Person on B and have the class Person on A and rerefence the attributs and properties. The only think i know is that you need to have the partial classes on the same namespace. But i don't want this.

    I want to do this because i have the logic on the class Person (on library A). I don't want the logic of the application on the LINQtoSQL file because if i have to change from LINQtoSQL to Entiti Framework i just have to set the model and nothing else.

    Thanks for any advice or help
    Saturday, October 3, 2009 2:04 AM

Answers

  • Hello,

    Partial classes are like having two source files (or more) for a single class. The compiler simply merges the two files into one (using logic you would assume mostly) and then compiles the class as if the source was a single file. So for example you can't have partial classes spanning multiple assemblies. The partial class is a feature of the compiler alone, the runtime (assemblies, CLR and so on) doesn't know anything about them, it sees it as a single class.

    So you can't have the two parts in two different namespaces either.

    In your case it might work (but would be a bit fragile) to put everything in one assembly, same namespace and such, so that partial classes work for you. Then if you need to switch between LINQ to SQL and Entity Framework you generate the model using the same namespace and same entity names (and same property/column names and so on), and then it might work. But it's a bit fragile as you would need to keep the two models in sync (same names of everything).

    Thanks,
    Vitek Karas [MSFT]
    Tuesday, October 6, 2009 1:23 PM
    Moderator