Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework map struct/complex type without model properties

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.

like image 525
Acrotygma Avatar asked Aug 31 '25 22:08

Acrotygma


1 Answers

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

like image 88
phil soady Avatar answered Sep 03 '25 10:09

phil soady