ModelBuilder .HasKey documentation glitch RRS feed

  • General discussion

  • I'm building a data model with EF 4.1 Code First and Visual Basic.

    I was trying to define a composite key for a class with Fluent API. Didn't seem to get it right although the methods seem to be quite well documented and this should be really easy, basic stuff:

    modelBuilder.Entity(Of PersonAddress)().HasKey(Function(pa) New From {pa.PersonId, pa.AddressId})
    I'm quite new to lambda expresssions and anonymous types and I keep getting errors. I had to look documentation for lambda expressionsa and anonymous types. Fortunately I found that in MSDN.

    Then I noticed that EF 4.1 .HasKey documentation has an error as it states:


    keyExpression: A lambda expression representing the property to be used as the primary key. C#: t => t.Id VB.Net: Function(t) t.Id If the primary key is made up of multiple properties then specify an anonymous type including the properties. C#: t => new { t.Id1, t.Id2 } VB.Net: Function(t) New From { t.Id1, t.Id2 }

    The correct would be "...VB.Net: Function(t) New With {t.Id1, t.Id2}".

    So I corrected my code to

    modelBuilder.Entity(Of PersonAddress)().HasKey(Function(pa) New With {pa.PersonId, pa.AddressId})

    And it works just fine.

    I hope somebody would correct this small glitch in a future version.


    • Changed type Aki Siponen Tuesday, September 13, 2011 6:23 AM
    • Edited by Aki Siponen Tuesday, September 13, 2011 6:27 AM
    Tuesday, September 13, 2011 6:22 AM