You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Attribute (Data Annotations) mapping for composite primary keys #11003
I'd like to declare a primary key that consists of multiple columns only with the Key attribute, just like for all other keys and as it was supported in EF6. This is the only situation when I have to go to the OnModelCreating method in another file to find out about the basic structure of the entity.
Currently I have to write this scattered code outside of where the entity is defined:
I'd prefer to keep it simple and in a single place, the entity class:
class MyEntity
{
[Key, Column(Order = 0)]
public int KeyColumn1 { get; set; }
[Key, Column(Order = 1)]
public int KeyColumn2 { get; set; }
}
I could imagine it would be helpful to get rid of the slightly unintuitive Column attribute just for this and define the order where it belongs and is used, the Key attribute, like in [Key(Order = 1)].
I'm wondering why there isn't already an issue for this. I have found one or two issues that might be similar but I don't understand them well enough to consider them a match for this topic. Also I couldn't see this mentioned in the sparse information I could find about the upcoming EF Core 2.1 so I have to assume it's not included in the next version.
Reacted by AK, Alex Wiese, Bongho Lee, Cengiz Ilerler, Vadim Ovchinnikov, matburton, Jez, Sean Terry, Craig Brown, Richard Lawley and 10 more
@ygoe We have been tracking the general issue of new attributes for things like this in issue #10111. However, I will leave this open as a specific case we should cover.
@ajcvickers in my case database may have new fields, and time to time I need to add those to code. The easy workaround for me is generating the new DbContext through scaffolding and compare it with the previously generated one.
Since scaffolding uses .HasKey instead of [Key], OData is not able to identify those and forcing me to use it in GetEdmModel() as below, which requires manual verification each time.
namespaceMyProject.Models{[Table("Items")]publicpartialclassItem{[Column("ID")]publiclongId{get;set;}publicstringNotes{get;set;}}[Table("ProcessorSettings")]publicpartialclassProcessorSettings{[StringLength(50)]publicstringName{get;set;}// <= IF it would set the [Key] for this property, I wouldn't need to define it later[StringLength(2)]publicstringCode{get;set;}[StringLength(500)]publicstringDescription{get;set;}}}
@cilerler Can you show the attributes that would be needed by OData to configure this as a composite key, including how the order of the two parts of the composite key is defined?
@cilerler That is something we can never support because the order of the properties in the composite key is critical for correctness. I'm actually very surprised that OData allows this given they they presumably have the same problem since the order is also critical to the OData model, but maybe it is constrained in some other way that isn't apparent here.
maybe it is constrained in some other way that isn't apparent here.
Based on stackoverflow post, OData is actually not caring about order at all. It's all about which are the key properties which should be bound when calling Get method. Order of properties does not matter because ODataRoute attribute specify which parameter should be bound with which key property.
@cilerler - You are free to add KeyAttribute to your model to make OData work. The EF model will not complain since Fluent API is overriding data annotation based configuration.
We will never scaffold KeyAttribute when generating EntityType from database until the ordering of properties issue is resolved out.
The OData issue does not change anything in this issue.
We will never scaffold KeyAttribute when generating EntityType from database until the ordering of properties issue is resolved out.
I was under the impression that I was asking if you may make the issue priority, did I sound different?
I got into the details because of @ajcvickers comments. As I mentioned above, it already works the way it is, and I have to do a manual check about the keys each time I do scaffolding which is an error-prone step.
Just wanted to nudge to get attention on this issue. 😁
changed the title [-]KeyAttribute support for composite primary key[/-][+]Attribute (Data Annotations) mapping for composite primary keys[/+]on Apr 18, 2022
Activity
ajcvickers commentedon Feb 21, 2018
@ygoe We have been tracking the general issue of new attributes for things like this in issue #10111. However, I will leave this open as a specific case we should cover.
cilerler commentedon May 9, 2019
@ajcvickers this issue becomes a big deal upon OData usage, is there any chance to bump the priority up.
ajcvickers commentedon May 9, 2019
@cilerler Can you provide some more details as to why OData changes things here?
cilerler commentedon May 9, 2019
@ajcvickers in my case database may have new fields, and time to time I need to add those to code. The easy workaround for me is generating the new DbContext through scaffolding and compare it with the previously generated one.
Since scaffolding uses
.HasKeyinstead of[Key], OData is not able to identify those and forcing me to use it inGetEdmModel()as below, which requires manual verification each time.EF
OData
ajcvickers commentedon May 9, 2019
@cilerler I don't see any composite keys in that example.
cilerler commentedon May 9, 2019
@ajcvickers updated as
.HasKey(e => new { e.Name, e.Code });ajcvickers commentedon May 9, 2019
@cilerler Can you show the attributes that would be needed by OData to configure this as a composite key, including how the order of the two parts of the composite key is defined?
cilerler commentedon May 9, 2019
@ajcvickers Just having
[Key]on properties would be fine, there is no order so far. You may see more details in here https://stackoverflow.com/a/27538581/439130ajcvickers commentedon May 9, 2019
@cilerler That is something we can never support because the order of the properties in the composite key is critical for correctness. I'm actually very surprised that OData allows this given they they presumably have the same problem since the order is also critical to the OData model, but maybe it is constrained in some other way that isn't apparent here.
smitpatel commentedon May 9, 2019
Based on stackoverflow post, OData is actually not caring about order at all. It's all about which are the key properties which should be bound when calling Get method. Order of properties does not matter because ODataRoute attribute specify which parameter should be bound with which key property.
@cilerler - You are free to add KeyAttribute to your model to make OData work. The EF model will not complain since Fluent API is overriding data annotation based configuration.
We will never scaffold KeyAttribute when generating EntityType from database until the ordering of properties issue is resolved out.
The OData issue does not change anything in this issue.
ajcvickers commentedon May 9, 2019
Thanks @smitpatel for clarifying that.
cilerler commentedon May 9, 2019
Thanks, @smitpatel.
I was under the impression that I was asking if you may make the issue priority, did I sound different?
I got into the details because of @ajcvickers comments. As I mentioned above, it already works the way it is, and I have to do a manual check about the keys each time I do scaffolding which is an error-prone step.
Just wanted to nudge to get attention on this issue. 😁
16 remaining items
Attribute for configuring composite primary keys
Attribute for configuring composite primary keys
Attribute for configuring composite primary keys
[-]KeyAttribute support for composite primary key[/-][+]Attribute (Data Annotations) mapping for composite primary keys[/+]