2

I'm having a similar error to: The property X is of type Y which is not supported by current database provider

but with a slightly different issue:

I have the following classes:

    public class Apple
    {
        public Box Box{ get; set; }

        public int R { get => Color.R; set => Color = new Color (value, Color.G, Color.B); }

        public int G { get => Color.G set => Color = new Color (Color.R, value, Color.B); }

        public int B { get => Color.B set => Color = new Color (Color.R, Color.G, value); }

        [NotMapped]
        public Color Color { get; set; }    
    }

    public class Box
    {
        [Key]
        public int ID { get; set; }

        public IEnumerable<Apple> Apples { get; set; }
    }

In the DbContext class:

        modelBuilder.Entity<Apple>()
            .HasKey(a=> new { a.Box, a.R, a.G, a.B });

But it does not seem to work and gives me the error: "The property 'Apple.Box' is of type 'Box' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'

fomi
  • 131
  • 3
  • 9
  • 2
    Why are you trying to annotate a complex type (i.e. `Box`) as part of a composite key? – CodeCaster Feb 24 '20 at 10:51
  • 1
    Does this answer your question? [Entity Framework Core Two Objects as Primary Key](https://stackoverflow.com/questions/50842509/entity-framework-core-two-objects-as-primary-key) – CodeCaster Feb 24 '20 at 10:54
  • @CodeCaster so I guess the basic answer is that a key must be a database type and not a CLR type. The error message unfortunately doesn't mention anything about keys which makes it confusing because it's possible to have other CLR types as properties as long as they're not keys. – fomi Feb 24 '20 at 11:13

1 Answers1

-3

A key must be a database type and not a CLR type. It's possible to have CLR types as properties as long as they're not keys.

fomi
  • 131
  • 3
  • 9