How to create an Entity Data Model for inherited generic types? - entity-framework

I have no clue how i can get an existing object structure based on the following classes (simplified) into a database using Entity Framework (EF is a constraint, i have to use it).
public abstract class WahWahProperty
{
public string Name { get; set; }
public abstract Type PropertyType { get; }
}
// ----------------
public class WahWahProperty<T> : WahWahProperty
{
public T Value { get; set; }
public override Type PropertyType
{
get { return typeof(T); }
}
}
// ----------------
public class WahWahContainer
{
public List<WahWahContainer> Children { get {...}; }
public List<WahWahContainer> Parents { get {...}; } // multiple "Parents" allowed
public List<WahWahProperty> Properties { get {...}; }
//... some more props here ...
}
Any ideas?

The EF doesn't support generic Entity types (which seems to be what you are doing).
Although we have made a change in EF 4.0 (not in Beta1) so you will be able to use a non-generic class derived from a generic class as an Entity.
Anyway hope this helps
Alex
Program Manager Entity Framework Team
Entity Framework Tips

Related

Map property using OwnsOne method in EF Core 2.0

I'm trying to migrate EF 6 project to EF Core 2.0.
I would like to streess out that I am not allowed to change anything in database structure - must be exactly the same as for EF 6 project.
I have the foloowing entities:
abstract class Vehicle { ... }
abstract class Car : Vehicle
{
public Dimension Dimensions { get; set; }
}
class Audi : Car { ... }
class Mazda : Car { ... }
class Dimension
{
public double Width { get; set; }
public double Height { get; set; }
}
Table mappings as follows:
public VehicleMap(EntityTypeBuilder<Vehicle> entityBuilder)
{
entityBuilder.ToTable("Vehicles");
entityBuilder.HasKey(_ => _.Id);
entityBuilder.HasDiscriminator<string>("Type").HasValue<Truck>(nameof(Truck));
}
public CarMap(EntityTypeBuilder<Car> entityBuilder)
{
entityBuilder.HasDiscriminator<string>("Type")
.HasValue<Mazda>(nameof(Mazda))
.HasValue<Audi>(nameof(Audi));
**entityBuilder.OwnsOne(_ => _.Dimensions);**
}
The problem I have is about Dimensions property defined in Car abstract class. I would like to map it using OwnsOne method to have all its class properties defined in the same table.
I'm getting the following error:
Cannot use table 'Vehicles' for entity type 'Car.Dimensions#Dimension'
since it has a relationship to a derived entity type 'Car'. Either
point the relationship to the base type 'Vehicle' or map
'Car.Dimensions#Dimension' to a different table.
Any idea how to solve it in EF Core 2.0?
The Dimension known as ValueObject. The best practice for this type of objects is here.
To implement this objects in your context first you should define it in base table, since when you create database with this configuration you have just one table with the name of Vehicles and discriminator column with the name of Type.
As the error say:
Either point the relationship to the base type 'Vehicle'
we should define Dimension in base class.
abstract class Vehicle
{
public Dimension Dimensions { get; private set; }
}
Table mappings:
public VehicleMap(EntityTypeBuilder<Vehicle> entityBuilder)
{
entityBuilder.ToTable("Vehicles");
entityBuilder.HasKey(_ => _.Id);
entityBuilder.HasDiscriminator<string>("Type").HasValue<Truck>(nameof(Truck));
// Here
entityBuilder.OwnsOne(p => p.Dimensions);
}
I would recommend a base ConfigurationBuilder for your Car class. Than you can make a configuration class (http://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-code-first.aspx)
Than you can do something like this:
public class CarConfiguration<TBase> : IEntityTypeConfiguration<TBase>
where TBase : class, Car
{
public virtual void Configure(EntityTypeBuilder<TBase> builder)
{
builder.OwnsOne(e => e.Dimensions);
}
}
After that you can use a configurationClass that derives from CarConfiguration foreach item that derives from car e.g.
public class AudiConfiguration : CarConfiguration<Audi>
{
public override void Configure(EntityTypeBuilder<Audi> builder)
{
base.Configure(builder); //call this to use configuration from Car
builder.....
}
}

AutoMapper: Mapping objects with interface properties

my current task needs to pay attention on mapping between different object types and so I recognized the very nice AutoMapper library.
So far easy to handle but these different objects contains complex interface type properties. Let me show you a code snippet:
public inferface IInterface
{
string TextProperty { get; set;}
}
public class A : IInterface
{
string TextProperty { get; set; }
}
public class B : IInterface
{
string TextProperty { get; set; }
}
public inferface IComplexInterface
{
IInterface ComplexProperty { get; set; }
}
public class ComplexA : IComplexInterface
{
IInterface ComplexProperty { get; set; }
}
public class ComplexB : IComplexInterface
{
IInterface ComplexProperty { get; set; }
}
In my case it is possible that class A is mapped to class B and vice versa.
Mapping from type A to B is no problem by configuring CreateMap<A, B>();
Mapping from class ComplexA to class ComplexB throws an exception:
Error mapping types.
Mapping types:
ComplexA -> ComplexB
NamespaceOfComplexA.ComplexA -> NamespaceOfComplexB.ComplexB
Type Map configuration:
ComplexA -> ComplexB
NamespaceOfComplexA.ComplexA -> NamespaceOfComplexB.ComplexB
Property:
ComplexProperty
A possible solution I already found here on stackoverflow could be a configuration as follows:
CreateMap<A, IInterface>().As<B>();
CreateMap<B, IInterface>().As<A>();
But in my case it is not working.
Any suggestions?
Now, I found a solution that works for me.
I use AutoMapper with a non generic approach and so I configure via:
CreateMap(typeof(ComplexA), typeof(ComplexB))
To consider properties with complex types like interfaces or even abstract classes it is possible to write an own ValueResolver that has to implement the interface:
IValueResolver<object, object, object>
with following method:
public object Resolve(object source, object destination, object destMember, ResolutionContext context)
{
//...
}
To resolve interface/abstract class properties you can configure your types by enhancing the configuration with the method ForMember(...) and define a conrete ValueResolver for the particular property as follows:
CreateMap(typeof(ComplexA), typeof(ComplexB)).ForMember("ComplexProperty", x => x.ResolveUsing(new ValueResolver(/*...*/)));
In my case it was the solution to map the interface property to a concrete implementation of my class definitions.
Hope it is useful.

Code first Type per hierarchy

I use EF 5 with the code first approach. Now I try to define a "code" table in which I want to have several different codes (like address code, medium code, etc.). In this table I just have the following properties: ID (Guid), Name (String), Description (String) and a discriminator (in this case something like the type of the code: address code, medium code, etc.).
So I defined the following base class:
public abstract class Code : EntityBase
{
public string Name { get; set; }
public string Beschreibung { get; set; }
}
Then I derived two classes from code
public class AddressCode : Code {}
public class MediumCode : Code {}
The class EntityBase is abstract and just defines the Id property, we use it for every POCO class...
The goal is that I can use AddressCode as a property on my address POCO class:
public class Adresse : EntityBase
{
#region Properties
public string Name1 { get; set; }
public virtual AddressCode AddressCode { get; set; }
#endregion
}
The question now is, how can I explain EF how to do that? Anyone can help?
Thanks
Marco
Thanks for your answer!
I tried to do it like you said. Unfortunately I get an error because of my EntityBase class:
public abstract class EntityBase
{
#region Properties
public virtual Guid Id { get; set; }
public virtual bool IsValid
{
get
{
{
return Validate();
}
}
}
[NotMappedAttribute]
public virtual IList<ValidationFailure> ValidationFailures { get; set; }
#endregion
#region Methods
private bool Validate()
{
var validatorFactory = new AttributedValidatorFactory();
IValidator validator = validatorFactory.GetValidator(GetType());
if (validator == null)
{
return true;
}
ValidationResult validationResult = validator.Validate(this);
ValidationFailures = validationResult.Errors;
return validationResult.IsValid;
}
#endregion
}
The error message is:
You cannot use Ignore method on the property 'ValidationFailures' on type 'Entities.AdresseCode' because this type inherits from the type 'Entities.EntityBase' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type.
As you can see I already defined the property ValidationFailures as NotMapped but still I get this error.. Do you have an idea?
Thanks
Marco
Just create a context (derived from DbContext)
public class AddressesDb : DbContext
{
public DbSet<Code> Codes { get; set; }
public DbSet<Adresse> Adressen { get; set; }
}
And (when used in code) EF will create a database with default table and column names. It will create a discriminator column of type text (nvarchar) which will contain the names of the classes that derive from Code.
If you want different names and/or types you should either use data annotations or fluent API to configure these.
Finally I got it work!
In the DBContext be aware to define DbSets for the code derived classes before all the other POCO's and then it works!

Abstract Properties with a foreign key in Entity Framework 4.1?

UPDATE:
I updated the classes to look more like my actual model. When I remove the Assignable property from the Document base entity, everything works.
Is something like this possible to map in EF 4.1?
public abstract class Entity
{
public Guid Id {get;set;}
}
public abstract class Assignable:Entity
{
}
public class Contact: Assignable
{
public string Name {get;set;}
}
public class Partner: Assignable
{
public string Area {get;set;}
}
public abstract class Document: Entity {
public Guid AssignedToId {get;set}
public Assignable AssignedTo {get;set;
}
public class Submittal: Document
{
public string SomeProperty {get;set;}
}
Mapping:
public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : Entity
{
protected EntityConfiguration()
{
HasKey(e => e.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
public class AssignableEntityMapping<TEntity>: EntityConfiguration<TEntity> where TEntity: Assignable
{
}
public class DocumentEntityMapping<TEntity>: EntityConfiguration<TEntity>
{
public DocumentEntityMapping()
{
HasOptional(e => e.AssignedTo).WithMany().HasForeignKey(e => e.AssignedToId);
}
}
public class ContactMapping: AssignableEntityMapping<Contact>
{
....
}
public class PartnerMapping: AssignableEntityMapping<Partner>
{
....
}
public class SubmittalMapping: DocumentEntityMapping<Submittal>
{
....
}
If this is possible how do I setup the mapping so that the AssignedToId is a foreign key to one of the concrete entities? I created a mapping for the Document entity and I get the error: "The property 'Id' is not a declared property on type..." I am guessing it is because EF doesn't know how to discriminate what the AssignedToId refers to?
I had a lot of extra architecture around the Contact and Partner entities that I could not use something like a TPH (Table Per Hierarchy) approach. EF doesn't know how to map the AssignedToId in the Document class since i was trying to use Table per Concrete Type (TPC) at the same time. I ended up just adding all the "assignable" types to the Document class for now. If there is another way around this, I'd like to know.
public abstract class Document: Entity
{
public Guid? AssignedContactId {get;set;}
public Contact AssignedContact {get;set;}
public Guid? AssignedPartnerId {get;set;}
public Partner AssignedPartner {get;set;}
}

ADO EF Code First Generic Intermediate Class Inheritance mapping

I've got the following requirement that works well in the OO space but I can't seem to get it to map back to the DB using ADO EF code first.
I have numrous products each will have different aspects (attributes but not in the sense of code attributes). For instance ring would have aspects such as mineral type = gold etc whilst a diamond would have an aspec of clarity = VVSI1.
As you can see the products very greatly in thier composition and I want a dynamic way of growing my system.
As such I've created a product class:
public class Product
{
public int id { get; set; }
public string Name { get; set; }
private List<ProductAspect> aspects = new List<ProductAspect>();
public List<ProductAspect> Aspects { get { return aspects; } set { aspects = value; } }
}
It has a list of ProductAspect which is the base class for all aspects moving forward:
public class ProductAspect
{
public int id { get; set; }
public string AspectName { get; set; }
}
I then inherit from the ProductAspect using a generic which alows me to be specific (strongly typed) about my Aspect Value:
public abstract class ProductAspect<T> : ProductAspect
{
public T AspectValue { get; set; }
}
I then create some Aspects that will allow me to decorate my product:
public class StringAspect : ProductAspect<string> { };
public class DecimalAspect : ProductAspect<decimal> { };
public class ImageAspect : ProductAspect<byte[]> { };
I then give the DbContext a try and have tried both TPH and TPC inheritance mappings.
Neither seem to work. The DB model that get's generated doesn't create a foriegn key to the StringAspect or DecimalAspect tables from the Aspect Table.
public class IxamDataContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductAspect> Aspects { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
AspectMapping(modelBuilder);
}
private void AspectMapping(DbModelBuilder mb)
{
//TPH
//mb.Entity<ProductAspect>()
// .Map<StringAspect>(m => m.Requires("type").HasValue("sa"))
// .Map<DecimalAspect>(m => m.Requires("type").HasValue("da"));
//TPC
//mb.Entity<StringAspect>().ToTable("StringAspect");
//mb.Entity<DecimalAspect>().ToTable("DecimalAspect");
}
}
Resulting in the following exception for this Seeding code:
Product p = new Product();
p.Name = "Diamond";
p.Aspects.Add(new StringAspect() { AspectName = "History", AspectValue = "Old and long" });
p.Aspects.Add(new DecimalAspect() { AspectName = "Weight", AspectValue= 96.5M });
context.Products.Add(p);
context.SaveChanges();
Excpetion:
EntityType 'StringAspect' does not
exist in the EntitySet
'IxamDataContext.Aspects'. Parameter
name: entity
Any ideas from the EF code first pros out there?
Entity framework doesn't support intermediate non mapped types in inheritance hierarchy. It means that you can't have this inheritance: A (mapped) -> B (not mapped) -> C (mapped). EF also doesn't support mapping generic types. It means that you must remove your generic intermediate class from the hierarchy and move AspectValue to derived types with correct type.
Maybe it's to late, but I would offer you using ComplexType attribute it will allows you to extend your types as you wish.