EF 6.3: It creates the same migration for changing from DataAnnotation to FluentAPI
EF 6.3: It creates the same migration for changing from DataAnnotation to FluentAPI
I have data annotation:
[Required]
[MaxLength(150)]
[Index(IsUnique = true)]
public string GuidName get; set;
Now we need to move it to Fluent API (and don't ask me why).
My code:
this.Property(c => c.GuidName).IsRequired().HasMaxLength(150);
this.HasIndex(c => c.GuidName).IsUnique(true).IsClustered(false);
It generates the following migration:
public override void Up()
DropIndex("dbo.Companies", new "CompanyUniqueString" );
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
public override void Down()
DropIndex("dbo.Companies", new "CompanyUniqueString" );
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
as we can see, it does the same and has the same code in Up and Down. But why is it generated at all?
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);– Oleg Sh
Aug 31 at 0:14
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
1 Answer
1
You have removed Index data annotation from the field, that's why do you have DropIndex(...) line generated in the Up() method and corresponding CreateIndex(...) line in the Down() method. At the same time, you have added index via Fluent API, it gives you the rest (CreateIndex(...) in the Up() method and DropIndex(...) in the Down()).
Index
DropIndex(...)
Up()
CreateIndex(...)
Down()
CreateIndex(...)
Up()
DropIndex(...)
Down()
So, EF detects two changes in the model and doesn't check if Fluent API produces exactly the same index as removed data annotation.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What migration was generated before these changes?
– Bagdan Gilevich
Aug 31 at 0:07