9

It seems use the Fluent API has more flexibility.

So I choose the way to follow always use the Fluent API to determine the feature it have in db instead use the annotations to.

But here is the problem,I couldn't find the way to set the Minimum length. Is there a method to perform this?

And i notice,there are no much topic discuss this way.Is the Fluent API way popular??

Hope we choose the right side.

RAM
  • 2,257
  • 2
  • 19
  • 41
葛厚德
  • 145
  • 3
  • 9
  • 4
    In must databases, if not all, does not exist minimum length constraint on string columns, so no point to add it to Fluent mapping api. – E-Bat Dec 30 '15 at 17:03
  • I get the point.I was confused with the database's column spec and the POCO property limit.Thanks~ E-Bat – 葛厚德 Dec 31 '15 at 01:53

3 Answers3

7

This is impossible at the moment with EF. If you really have to set a minimum length, you can do it with an attribute though:

[MaxLength(10),MinLength(3)]
public string MyProperty {get; set;}

As the first comment under your question already says, it's probably not very common to have minimum length check in databases (never seen it myself), so this will just throw a validation error when you try to enter a value with a length smaller than 3.

Alexander Derck
  • 13,818
  • 5
  • 54
  • 76
  • Thanks for the answer,Alex.But I still curious about the affect in database after I append the annotation. Is it ok in the same time use the annotation and the Fluent API ? Is the annotation just easy for validate the function parameters ? – 葛厚德 Dec 31 '15 at 02:10
  • 1
    Yes using the annotations and fluent api together isn't bad. I try to do validation of properties as much as possible with annotations because it's clearer to see when you look at your models. The relationships between entities I try to keep as much as possible in fluent api. – Alexander Derck Dec 31 '15 at 12:47
  • Pardon me, I was reading *maximum*, this answer is indeed correct. The fluent API is not there to validate (although it does that *too*), it's there to configure the DB schema, and there is no "minimum length" allowed in SQL server (which is basically what EF6 supported till EF7), so that's why it's not there. What EF7 will bring in the future is still a mistery :-) – Jcl Jan 07 '16 at 16:12
2

For string property use StringLength

[StringLength(30, MinimumLength = 5)]
public string MyStringProperty { get; set; }

https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute%28v=vs.110%29.aspx

Cyrus
  • 2,261
  • 2
  • 22
  • 37
0

Maybe late reply for this question still there is no way to specify using Fluent API.

But We can do from Check constraint using Fluent API to make sure Data length is greater than some length.

builder.HasCheckConstraint("CHK_DocumentLogo_ExtensionHasAtleastOneChar", "(DATALENGTH([Extension]) > 0)");

you need to install the Microsoft.EntityFrameworkCore.Relational NuGet package to use this extension method.

More info on HasCheckConstraint - https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationalentitytypebuilderextensions.hascheckconstraint?view=efcore-5.0

RashmiMs
  • 129
  • 14
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '22 at 13:17