1

Working on a WPF project using entity Framework 6.

My custom class includes 2 fields that would need to be unique (on top of id). One is a string type, the other one is a custom type.

The implementation of the entity class is s follows:

public partial class MyEntity
{
    [Key]
    public int Id { get; set; }

    [MaxLength(100)]
    [Index(IsUnique = true)]
    public string Name{ get; set; }

    [MaxLength(100)]
    [Index(IsUnique = true)]
    public MyClass myClass { get; set; }
...
}

Then, when I tray to add a new MyEntity via:

context.SaveChanges();

I get this exception:

  Message=Unable to create a constant value of type 'NS.MyClass '. Only primitive types or enumeration types are supported in this context.
  Source=EntityFramework

NB: the other unique field (Name) does not generate the exception if I comment the attribute of the MyClass declaration.

Is there really no way to set a custom class as Unique into an entity??

Thx in advance.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
A.D.
  • 1,062
  • 1
  • 13
  • 37

1 Answers1

0

In Entity Framework 6 you have to do this by your self. check my soultion here: Unique Key constraints for multiple columns in Entity Framework

Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70