Entity Framework map struct/complex type without model properties

c# entity-framework entity-framework-6 fluent

Question

I have a 'model struct' which is internally a string, but is being used to somewhat similiar to a GUID.

public struct Token {

    private string _value;

    private Token(Guid uuid) {
        _value = Token.FromGuid(uuid);
    }

    public static Token FromGuid(Guid uuid) {
        // perform 'transformation'
        // stuff
        // return...
    }

    // other static methods to create a token...

}

How can I map this entity using Entity Framework 6 Code First? I'm aware that structs are not supported, but a complex type also doesn't seem applicable since the class doesn't contain any properties.

Popular Answer

Structs are not a supported EF type. See ef supported primitives

You will need to use a complex type instead.

  • modelBuilder.ComplexType<Details>();
    or
  • Data Annotation [ComplexType]

But consider the alternative using a string and managing the public get/set accordingly




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why