1

I am using MVC Scaffolding + EF6 in a Web applicatioon project in VS 2013.

Domain classes (Entites) and the context (DbContext) are in two separate projects referenced by the Web project.

I have a Patient class which has a complex property like follows.

public class Patient
{
    public int PatientId { get; set; }

    // Some properties

    // Complex property
    public MyComplexType Complex { get; set; }
}

public class MyComplexType
{
    public SomeType Property1 { get; set; }
    public SomeOtherType Property2 { get; set; }
}

Problem:

MVC scaffolding engine does not detect the complex property in Patient class and generated views don't contain fields to show or edit that property. I tried decorating MyComplexType class with ComplexType attribute but it didn't work.

What can be done?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kamran
  • 782
  • 10
  • 35

1 Answers1

0

According to this post and Julie Lerman's book Programming Entity Framework: Code First, complex types can only contain primitive properties.

Conventional Complex Type Rules

  1. Complex types have no key property.
  2. Complex types can only contain primitive properties.
  3. When used as a property in another class, the property must represent a single instance. It cannot be a collection type.

In my case I am using an unconventional complex type so I should have decorated MyComplexType class with ComplexType attribute too.

public class Patient
{
    public int PatientId { get; set; }

    // Some properties

    // Complex property
    public MyComplexType Complex { get; set; }
}

[ComplexType]
public class MyComplexType
{
    public SomeType Property1 { get; set; }
    public SomeOtherType Property2 { get; set; }
}


public class SomeType
{
    // primitive  properties here
}

public class SomeOtherType 
{
    // primitive  properties here
}

HTH

Community
  • 1
  • 1
Kamran
  • 782
  • 10
  • 35