Convention-based model builder support for Data Annotations
We should try to support at least the very basic ones, e.g. KeyAttribute for alpha, provided that the DataAnnotations build is ready for K. Attributes to be implemented:
- [x] Key
- [x] Required
- [x] MaxLength
- [x] NotMapped
- [x] ConcurrencyCheck
- [x] TimeStamp
- [x] Table
- [x] Column
- [x] DatabaseGenerated
- [x] StringLength
Relationship
- [x] ForeignKey
- [x] InverseProperty
- [x] Required on Navigations
Not supported yet
- ComplexType
@smitpatel please talk to @AndriySvyryd about the details.
I ran into another scenario where this issue causes problems.
Because .MaxLengh() isn't being honored on string properties, nvarchar(MAX) fields are generated in the db. Unique constraints aren't supported on nvarchar(MAX) fields, so an error is thrown during db generation not for applying the .MaxLength() annotation, but when you apply a Unique constraint on a string property where you think you've limited the length.
:+1:
Please also provide annotation for uniqueness constraint as a model convention, so we don't have to "drop down" to manual model builder configuration probably like so:
[Unique]
public int MySurrogateKey { get; set; }
// here MySurrogateKey can be null as a unique value
[UniqueNotNull]
public int MySurrogateKey { get; set; }
// here MySurrogateKey can not be null
[UniqueAllowNull]
public int MySurrogateKey { get; set; }
// here MySurrogateKey can be null multiple times, but non-null values should be unique
copy: @Tratcher, @davidfowl, @NTaylorMullen
filed #2504 & #2505 Attributes to be implemented has been added in first post. We need to decide which relationship attributes we want to support.
We should support StringLength which is a synonym of MaxLength only applicable to strings.
+1 for [StringLength]
We also need to discuss [ForeignKey] and [InverseProperty]. My gut feel is we should probably support them with the same rules we had in EF6.
+1 for [ForeginKey] and [InverseProperty]
All the attributes mentioned in first post are implemented. Anything else remaining here?
This should be sufficient as the initial implementation. We'll file separate issues for remaining work.
How about the ability to change column name?
[Column(Name = "")]
@salarcode - ColumnAttribute has been implemented already. You can change column name using the method you have mentioned. :smile:
What about Index attribute? Defining non-unique indexes very popular/important feature. But I can't find issue about it.
@justdmitry In EF7 we support defining indexes using the fluent API but not an attribute, at least no yet. The IndexAttribute you are possibly referring to is something we added to the EF 6.x package at some point but never really became a standard DataAnnotation.
We don't want to copy the original attribute from EF6 as is because there are a few things in it that we would like to change. Also, having it in DataAnnotations directly would likely make more sense than adding it to the EF7 package.
The priority is going to be driven by customer feedback.
The priority is going to be driven by customer feedback.
I should mention though that it is highly unlikely that we will add IndexAttribute in the EF7 RTM timeframe.
+1 for IndexAttribute I don't know what changes you think of, but the Index itself and the IsUnique property is essential in my opinion, other properties are not that much.
+1 for IndexAttribute. I prefer to use DataAnnotations over FluentAPI whenever its possible. Keeps the code easier to understand.
I've always thought that something like
entity.Property(e => e.ShipDate).DefaultValueSql("getutcdate()");
should instead be (or at least support) something like this;
[DefaultValueSql("getutcdate()")]
public DateTime ShipDate { get; set; }
Would this be considered for inclusion, or is it something an extension library should offer?
+1 for IndexAttribute
+1 for IndexAttribute, that was nice feature. Of course in parallel with "fluent API" implementation.
+1 for IndexAttribute