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 ?

tchelidze
  • 8,050
  • 1
  • 29
  • 49
  • What is `ValueObject`? I think the error has to do with the inheritance. `Nationality` as such should be interpreted as value object (`ComplexType` in EF terms). – Gert Arnold Mar 19 '17 at 15:25
  • @GertArnold it's [ASP.NET Boilerplate Framework defined type](http://www.aspnetboilerplate.com/Pages/Documents/Value-Objects) – tchelidze Mar 19 '17 at 15:48
  • @GertArnold Sure, i'm using EF version `6.1.3`. – tchelidze Mar 19 '17 at 15:59
  • I can use your code without problems in EF6, that is: with `ValueObject` and `Entity` from ABP. As a last resort, try `modelBuilder.ComplexType();`. – Gert Arnold Mar 19 '17 at 16:07
  • @GertArnold `ComplexType` is what i ended up doing, but it denormalized database structure, `Nationality`'s columns into `Employee`'s table. – tchelidze Mar 19 '17 at 16:36
  • Well, that's the idea of a value object, as per the link you shared. If you want it to be normalized you have to use Nationality as an entity. – Gert Arnold Mar 19 '17 at 16:40

1 Answers1

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.

prabakar
  • 11
  • 2