2

Is there any way to rename a primary/foreign key constraint name through either data annotations or fluent API?

I'm not talking about the property/column name. The constraint name. I did some search and didn't find any solutions yet.

Please help!

renjith
  • 423
  • 5
  • 12
  • You can do this in an `Up` [migration](http://stackoverflow.com/a/17929867/314291) – StuartLC Nov 12 '15 at 06:07
  • Yeah. I know. But is that the only way? I mean there's gotta be some way through fluent API when defining primary keys right? – renjith Nov 12 '15 at 06:08
  • 1
    Seemingly you can also go as far as [overriding the SqlGenerator conventions](http://stackoverflow.com/a/31553476/314291) – StuartLC Nov 12 '15 at 06:10
  • @StuartLC thanks! That'll work, although that's a bit code overload. I was hoping for a simple solution. Migration is the way..! – renjith Nov 12 '15 at 06:19

2 Answers2

1

with .HasConstraintName like you can see in the underlying example

entity.HasOne(d => d.Director)
                    .WithMany(p => p.Movie)
                    .HasForeignKey(d => d.DirectorId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Movie_Person");
Nick
  • 11
  • 1