efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Convention-based model builder support for Data Annotations

Open divega opened this issue 12 years ago • 27 comments

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

divega avatar Apr 22 '14 19:04 divega

@smitpatel please talk to @AndriySvyryd about the details.

divega avatar Jan 27 '15 22:01 divega

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.

pauldalyii avatar Apr 09 '15 17:04 pauldalyii

:+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

ghost avatar May 01 '15 14:05 ghost

filed #2504 & #2505 Attributes to be implemented has been added in first post. We need to decide which relationship attributes we want to support.

smitpatel avatar Jun 30 '15 00:06 smitpatel

We should support StringLength which is a synonym of MaxLength only applicable to strings.

divega avatar Jun 30 '15 01:06 divega

+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.

rowanmiller avatar Jun 30 '15 17:06 rowanmiller

+1 for [ForeginKey] and [InverseProperty]

divega avatar Jun 30 '15 20:06 divega

All the attributes mentioned in first post are implemented. Anything else remaining here?

smitpatel avatar Aug 12 '15 23:08 smitpatel

This should be sufficient as the initial implementation. We'll file separate issues for remaining work.

AndriySvyryd avatar Aug 13 '15 20:08 AndriySvyryd

How about the ability to change column name?

[Column(Name = "")]

salarcode avatar Aug 26 '15 10:08 salarcode

@salarcode - ColumnAttribute has been implemented already. You can change column name using the method you have mentioned. :smile:

smitpatel avatar Aug 26 '15 18:08 smitpatel

What about Index attribute? Defining non-unique indexes very popular/important feature. But I can't find issue about it.

justdmitry avatar Aug 26 '15 19:08 justdmitry

@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.

divega avatar Aug 28 '15 23:08 divega

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.

divega avatar Aug 28 '15 23:08 divega

+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.

salarcode avatar Aug 29 '15 05:08 salarcode

+1 for IndexAttribute. I prefer to use DataAnnotations over FluentAPI whenever its possible. Keeps the code easier to understand.

farlop avatar Sep 11 '15 07:09 farlop

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?

staff0rd avatar Sep 12 '15 15:09 staff0rd

+1 for IndexAttribute

ghk avatar Sep 12 '15 21:09 ghk

+1 for IndexAttribute, that was nice feature. Of course in parallel with "fluent API" implementation.

Yorie avatar Sep 28 '15 16:09 Yorie

+1 for IndexAttribute

yukozh avatar Oct 02 '15 23:10 yukozh