0

Is Autofixture capable of creating an entity depending upon the mapping configuration done using EF fluent API? I am looking more specifically where a string has a certain length been configured but Autofixutre is generating string with longer length.

-- Updated question -- Let's take an example of a User entity with UserName property whose length is fixed to 8 character. The configuration of UserName Property is done using EF Fluent API, something like this:

modelBuilder.Entity<User>().Property(t => t.UserName).HasMaxLength(8);

Now when I instantiate User entity using Autofixture it gives me a string of UserName with longer than 8. This will trigger an exception while trying to save this entity into db. There are lot of properties with different configuration, which is preventing me to take the full benefit of using Autofixture.

Sunil Munikar
  • 25
  • 2
  • 6

1 Answers1

0

AutoFixture doesn't know anything about your ORM. So there is no built-in support for EF configuration. It does indeed support a bit of System.ComponentModel.DataAnnotations attributes.

I guess only 3 of them are supported by AutoFixture:

  • StringLengthAttribute
  • RegularExpressionAttribute
  • RangeAttribute

So for you particular example you could add [StringLength] to your UserName property instead of using Fluent API.

For more support you would need an extension that can read your EF configuration and configure AutoFixture with EF constraints. There were some efforts in this direction but seems it's harder than it sounds.

Look here - https://github.com/AutoFixture/AutoFixture/issues/162

FunkyOne
  • 553
  • 4
  • 9
  • even though your answer doesn't answer the question it points to the correct direction (_some effort done but seem harder than it sounds_) – Sunil Munikar Feb 03 '16 at 21:41