Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasKey() on an overridden property ignores Column name attribute #22092

Closed
taisbak opened this issue Aug 17, 2020 · 2 comments · Fixed by #22208
Closed

HasKey() on an overridden property ignores Column name attribute #22092

taisbak opened this issue Aug 17, 2020 · 2 comments · Fixed by #22208
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@taisbak
Copy link

taisbak commented Aug 17, 2020

When specifying HasKey() for an overridden property the column name attribute is ignored.

Steps to reproduce

In the following code sample, toggle the HasKey() in DbContext.
When it's commented out - it works as expected.
When present - you get an exception: "Invalid column name 'ObjectId'."

It works in EF 3.1.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

using Microsoft.EntityFrameworkCore;

public class SysObjects
{
    public virtual int ObjectId { get; set; }
}

[Table("tables", Schema = "sys")]
public class SysTables : SysObjects
{
    [Key]
    [Column("Object_id")]
    override public int ObjectId { get; set; }
    public string Name { get; set; }
    public int Schema_id { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<SysTables> Tables { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var _connectionString = @"Server=(localdb)\mssqllocaldb;Database=Master;";
        optionsBuilder
            .UseSqlServer(_connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<SysTables>()
            .HasKey(e => new { e.ObjectId }) // comment out this line to bypass the bug
            ;
    }
}

class Program
{
    static void Main()
    {
        using var _dbContext = new MyContext();

        var Query =
            from t in _dbContext.Tables
            select new { t.ObjectId, t.Name };

        var TableInfo = Query.FirstOrDefault();
    }
}

### Further technical details

EF Core version: 5.0.0-preview.8.20365.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.8.0
AndriySvyryd added a commit that referenced this issue Aug 18, 2020
ghost pushed a commit that referenced this issue Aug 18, 2020
@ajcvickers
Copy link
Member

@AndriySvyryd I am able to reproduce this on a 5.0 daily. In 3.1, the table mapped is:

CREATE TABLE [dbo].[tables] (
          [Object_id] int NOT NULL IDENTITY,
          [Name] nvarchar(max) NULL,
          [Schema_id] int NOT NULL,
          CONSTRAINT [PK_tables] PRIMARY KEY ([Object_id])
      );

But in 5.0 it is:

CREATE TABLE [dbo].[tables] (
          [ObjectId] int NOT NULL IDENTITY,
          [Name] nvarchar(max) NULL,
          [Schema_id] int NOT NULL,
          CONSTRAINT [PK_tables] PRIMARY KEY ([ObjectId])
      );

@ajcvickers ajcvickers added this to the 5.0.0 milestone Aug 24, 2020
@AndriySvyryd AndriySvyryd removed their assignment Aug 25, 2020
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 25, 2020
@AndriySvyryd AndriySvyryd modified the milestones: 5.0.0, 5.0.0-rc1 Aug 25, 2020
@taisbak
Copy link
Author

taisbak commented Aug 25, 2020

Now it works for me too :-)
Thank you for swift resolution.

@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants