I have a base object that I dont want to be mapped in DB as an entity, I only want the properties to be added to the object that is mapped in the DB :
Not mapped object (dont know if it matters but baseobject is in another assembly):
public class BaseObject
{
public virtual string Prop1 { get; set; }
public virtual string Prop2 { get; set; }
}
Mapped object:
public class ChildObject : BaseObject
{
public virtual string Prop3 { get; set; }
public virtual string Prop4 { get; set; }
public virtual string Prop5 { get; set; }
}
What is registered in DbContext
public DbSet<ChildObject> ChildObjects { get; set; }
What i'd like to see in Db
table:ChildObject
col:Prop1 (from BaseObject)
col:Prop2 (from BaseObject)
col:Prop3
col:Prop4
col:Prop5
To resume, what I want to do, is to have one table in the Db that has the child and the base properties.
This is the error i'm currently getting:
The type 'namespace.ChildObject' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.
I've been digging around, but can't find how to do this.
Any ideas?
EDIT:
@Kyle Trauberman is right, however, for some reason there seems to be a problem with inheriting from a base class in different assembly. I simply did this.
class BaseObjectClone : BaseObject { } /* BaseObject being in another assembly */
public class ChildObject : BaseObjectClone {
public virtual string Prop3 { get; set; }
public virtual string Prop4 { get; set; }
public virtual string Prop5 { get; set; }
}
Inheritance with EF Code First: Table per Hierarchy (TPH) Inheritance with EF Code First: Table per Type (TPT) Inheritance with EF Code First: Table per Concrete class (TPC)
By default, EF maps the inheritance using the table-per-hierarchy (TPH) pattern. TPH uses a single table to store the data for all types in the hierarchy, and a discriminator column is used to identify which type each row represents.
The Code First approach enables you to define an entity model in code, create a database from the model, and then add data to the database. MySQL Connector/NET is compatible with multiple versions of Entity Framework Core.
Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…
Morteza Manavi has a blog post detailing how to do this:
http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx
Basically, you'll need to override OnModelCreating
in your DbContext
and call MapInheritedProperties()
for each of the child tables.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<BankAccount>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("BankAccounts");
});
modelBuilder.Entity<CreditCard>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("CreditCards");
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With