4

Trying to implement DDD using ASP.NET Boilerplate and Entity Framework

Employee object

public class Employee : Entity<long>
{
   public virtual string Name { get; protected set; }

   public virtual Nationality Nationality { get; protected set; }
}

defines property Nationality, which is value object

public class Nationality : ValueObject<Nationality>
{      
    public virtual string Name { get; protected set; }
}

Attempt to add database migration produces obvious error

EntityType 'Nationality' has no key defined. Define the key for this EntityType.

Took a look at 3 different approach to persist value object in database

Should i implement one of the above mentioned method manually to persist ValueObject (if so what are some of the best practices) ? or it's already done by ASP.NET Boilerplate Framework ?

6
  • What is ValueObject<T>? I think the error has to do with the inheritance. Nationality as such should be interpreted as value object (ComplexType in EF terms). Commented Mar 19, 2017 at 15:25
  • @GertArnold it's ASP.NET Boilerplate Framework defined type Commented Mar 19, 2017 at 15:48
  • @GertArnold Sure, i'm using EF version 6.1.3. Commented Mar 19, 2017 at 15:59
  • I can use your code without problems in EF6, that is: with ValueObject<T> and Entity<T> from ABP. As a last resort, try modelBuilder.ComplexType<Nationality>();. Commented Mar 19, 2017 at 16:07
  • @GertArnold ComplexType is what i ended up doing, but it denormalized database structure, Nationality's columns into Employee's table. Commented Mar 19, 2017 at 16:36

1 Answer 1

1

when you use Value object should not use virtual on the Nationality property on the Employee object. Remove the virtual keyword. that should fix it. Use Virtual only when you reference an entity type. Also you can get rid of virtual on the Nationality.Name property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.