"The property is not supported by current database provider"

ef-core-3.1 entity-framework entity-framework-core

Question

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'

1
0
2/24/2020 10:45:46 AM

Accepted Answer

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.

0
3/3/2020 1:21:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow