How do I store a complex object from an external DLL?

I have a complex object from an external DLL (I cannot change the code of this object).

Let's take this class as an example. This class has no id and contains other complex classes.

public class Car : IVehicle
{
    public IWheel Wheel { get; set; }
    public IEngine Engine { get; set; }
    public Car(IWheel wheel, IEngine engine)
    {
        Wheel = wheel;
        Engine = engine;
    }
}

      

How do I store this object first with Entity Framework code?

+3


source to share


1 answer


Unfortunately, EntityFramework does not support objects with no primary keys other than complex types.

However, you do not need to create DTOs and transformations from third part library objects to these DTOs, you just need to create a subclass like CarEntity, then add the Id property to it:

class Car
{
    public string Name { get; set; }
}

class CarEntity : Car
{
    public int Id { get; set; }
}

      



Entity Framework maps this hierarchy correctly to one table, see the required migration step:

CreateTable(
    "dbo.CarEntities",
    c => new
        {
            Id = c.Int(nullable: false, identity: true),
            Name = c.String(),
        })
    .PrimaryKey(t => t.Id);

      

This way you can keep those objects with minimal extra work and reuse them in third party library functions that will expect car types as input, etc.

0


source







All Articles