Columns in SELECT statements are reversed when entity class inherits from a base class
See original GitHub issueI’m using the 5.0.0-preview2.
Just noticed something weird, not really a bug but it is annoying when analyzing SQL queries…
When the entity type inherits from another type, the columns are reversed in the SELECTs, here’s what I mean:
[Table(Name = "BsContact", IsColumnAttributeRequired = false)]
public class Contact
{
[PrimaryKey]
public int ContactId { get; set; }
public string ContactCode { get; set; }
public int? Age { get; set; }
public DateTime? DateBirth { get; set; }
}
generates this:
SELECT
[t1].[ContactId],
[t1].[ContactCode],
[t1].[Age],
[t1].[DateBirth]
FROM
[BsContact] [t1]
but this mapping:
public class BaseType
{
}
[Table(Name = "BsContact", IsColumnAttributeRequired = false)]
public class Contact : BaseType
{
[PrimaryKey]
public int ContactId { get; set; }
public string ContactCode { get; set; }
public int? Age { get; set; }
public DateTime? DateBirth { get; set; }
}
generates this:
SELECT
[t1].[DateBirth],
[t1].[Age],
[t1].[ContactCode],
[t1].[ContactId]
FROM
[BsContact] [t1]
Not really sure why it works with no base type, but the reverse order seems to originate from this method called by TypeAccessor<T>
, it returns members in reverse order because it uses AddFirst
and not AddLast
. Is there any reason for that?
Thanks in advance
Issue Analytics
- State:
- Created 7 months ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Wrong ordering in generated table in jpa
Hibernate generates columns in alphabetical order. According to this post the reason is given as: It is sorted to ensurce deterministic ...
Read more >Writing SELECT statements for Inheritance Mappings
When the SELECT statement is against the base class in the hierarchy, the default behavior is that only that class' table will be...
Read more >Chapter 2. Mapping Entities
2.2. An embeddable object inherits the access type of its owning entity (note that you can override that using the @Access annotation). The...
Read more >Tutorial: Implement inheritance - ASP.NET MVC with EF Core
This tutorial will show you how to implement inheritance in the data model, using Entity Framework Core in an ASP.NET Core application.
Read more >2.3 Inheritance in SQL Object Types
SQL object inheritance is based on a family tree of object types that forms a type hierarchy. The type hierarchy consists of a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@guillaume86
Thanks for doing the research! If you modify the code, it’d be nice to leave a comment to explicit that purpose.
I was referring to the
members
local variable that contains all the public fields/properties of the type we’re looking at. My remark was meant to say that depending on the ordering we eventually land on, we don’t need those sorting shenanigans and we only need to filter out members hidden bynew
fromGetMembers
.About sorting: it might be worth noting what MSDN says about
GetMembers
ordering anyway:Should be easy to fix. One of cases when tests-first works great. Just define hierarchy of test classes with properties, fields, new and interfaces and then define test over
GetPublicInstanceValueMembers
method for each hierarchy type and then make them work 😃