Reputation: 8043
The following simple code example illustrates the scenario in question. I have a Person entity, which is simply mapped to the Person table in the DB. I am using the default Entity Object code generator.
public partial class Person { ... }
I have a PersonDetail class deriving from Person, which contains some extra properties, which are needed by the GUI. None of these properties needs to be persisted in the DB, so I do not want to map this class to the database.
public class PersonDetail : Person
{
public int TheGuiNeedsThisInformation { get; set; }
}
And I send down instances of the Detail class the GUI with WCF. My problem is, if the GUI modifies some properties of the PersonDetail instance, and sends it back to the server to update it, I can not simply Attach it to my context, because the derived class is not mapped to any table ("Mapping and metadata information could not be found for EntityType PersonDetail"). I tried to map it to the same table as the Person class is mapped, but the EF throws some exception about the mapping that way.
I managed to workaround this by creating a copy of the PersonDetail instance by copying its property values to a new Person instance, and then saving it to the DB. However I would like to avoid that extra work. What am I doing wrong? Is this not possible? In Linq to SQL this worked like a charm, basically out of the box.
And if this is not possible, what is the suggested way to extend my entity classes with some extra information, which don't have to be persisted? (The obvious way would be to simply add extra properties to the partial Person class. However, i do not want to do that, because that way the GUI can never be sure whether the extra properties have been filled on the server or not.)
UPDATE: Thanks for the suggestions, however my main question is still open: Is it possible to have a base and a derived class (where the derived class does not have any additional properties which have to be saved to the DB) mapped to the same table, so that I can simply attach an instance of the derived type to my context and save it like if it was an instance of the base type? I could not do this yet, and I am surprised, because with Linq to SQL it simply worked.
Upvotes: 1
Views: 1517
Reputation: 6475
Have you tried using composition instead of inheritance?
public class PersonViewModel {
public Person Person { get; }
public int? MyViewProperty { get; set; }
// ...
}
Unless there's a really good reason I would never transfer that object to your service however. To me that looks like a client side object only. What's the point of having GUI properties transmitted to the server when they're not stored in the database anyway? Isn't it enough to just get
the Person and send that to the server for update?
Upvotes: 0
Reputation: 60516
You can add your properties to the class and "ignore" them for the entity framework using the modelBuilder, looks like you doing code first development. For that options have a look at the modelBuilder =>
public class Db : DbContext
{
public DbSet<MyClass> MyClasses{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyClass>().Ignore(x => x.MyProperty);
}
}
hope this helps!
Upvotes: 1
Reputation: 39898
I don't think inheritance is the right answer to this. Is it true that 'PersonDetail' is-a Person? I would choose for creating a PersonDetail class and adding a property in the partial class that points to an instance of the PersonDetail class.
That way you can check on your client side if the PersonDetail is set by the server by just checking if the property != null.
Upvotes: 1