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

Incorrect behavior of classes with the same name #23981

Closed
AR1ES opened this issue Jan 26, 2021 · 3 comments · Fixed by #25734
Closed

Incorrect behavior of classes with the same name #23981

AR1ES opened this issue Jan 26, 2021 · 3 comments · Fixed by #25734
Assignees
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

@AR1ES
Copy link

AR1ES commented Jan 26, 2021

I have two classes (keyless entities) with same names but different namespaces. Classes have the same properties with different signatures.

namespace ns1
{
  public class TestQuery
  {
    public int? MyValue { get; set; }
  }
}

namespace ns2
{
  public class TestQuery
  {
    public int MyValue { get; set; }
  }
}

DbContext:

  public class TestContext : DbContext
  {
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {

      var connectionString = "....";
      optionsBuilder.UseSqlServer(connectionString);
      optionsBuilder.EnableSensitiveDataLogging(true);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
      base.OnModelCreating(modelBuilder);
      var mb = modelBuilder.Entity(typeof(ns1.TestQuery));

      mb.HasBaseType((Type)null);
      mb.HasNoKey();
      mb.ToTable(null);

      mb = modelBuilder.Entity(typeof(ns2.TestQuery));

      mb.HasBaseType((Type)null);
      mb.HasNoKey();
      mb.ToTable(null);
    }
  }

Run Program

  class Program
  {
    static void Main(string[] args) {
      using var db = new TestContext();
      var good1 = db.Set<ns1.TestQuery>().FromSqlRaw(@"SELECT 1 AS MyValue").ToList(); // OK
      var good2 = db.Set<ns2.TestQuery>().FromSqlRaw(@"SELECT 1 AS MyValue").ToList(); // OK
      var bad = db.Set<ns1.TestQuery>().FromSqlRaw(@"SELECT cast(null as int) AS MyValue").ToList(); // Exception
    }
  }

At the last statement I get exception:

Data is Null. This method or property cannot be called on Null values.

   at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
   at Microsoft.Data.SqlClient.SqlBuffer.get_Int32()
   at Microsoft.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ConsoleApp4.Program.<Main>d__0.MoveNext() in ... Program.cs:line 18

ns1.TestQuery.MyValue is nullable, but exception says that Null is not allowed. As I suppose, there is some kind of cache that works by the name of the class without namespace.

EF Core version: 5.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10 Version 10.0.19042 Build 19042
IDE: Visual Studio 2019 16.8.3

@ajcvickers
Copy link
Member

@smitpatel @AndriySvyryd I am able to reproduce this. The exception only happens when both entity types are included in the model.

@smitpatel
Copy link
Member

If you have repro handy, can you print model also? That would help in narrowing down if model building is running into issue or query. Wrong type is getting picked up it seems which shouldn't happen in type based lookup.

@ajcvickers
Copy link
Member

@smitpatel

Model: 
  EntityType: TestQuery Keyless
    Properties: 
      MyValue (Nullable<int>)
        Annotations: 
          Relational:DefaultColumnMappings: System.Collections.Generic.SortedSet`1[Microsoft.EntityFrameworkCore.Metadata.Internal.ColumnMappingBase]
    Annotations: 
      ConstructorBinding: Microsoft.EntityFrameworkCore.Metadata.ConstructorBinding
      Relational:DefaultMappings: System.Collections.Generic.List`1[Microsoft.EntityFrameworkCore.Metadata.Internal.TableMappingBase]
      Relational:Schema: 
      Relational:TableName: 
      ServiceOnlyConstructorBinding: Microsoft.EntityFrameworkCore.Metadata.ConstructorBinding
  EntityType: TestQuery Keyless
    Properties: 
      MyValue (int) Required
        Annotations: 
          Relational:DefaultColumnMappings: System.Collections.Generic.SortedSet`1[Microsoft.EntityFrameworkCore.Metadata.Internal.ColumnMappingBase]
    Annotations: 
      ConstructorBinding: Microsoft.EntityFrameworkCore.Metadata.ConstructorBinding
      Relational:DefaultMappings: System.Collections.Generic.List`1[Microsoft.EntityFrameworkCore.Metadata.Internal.TableMappingBase]
      Relational:Schema: 
      Relational:TableName: 
      ServiceOnlyConstructorBinding: Microsoft.EntityFrameworkCore.Metadata.ConstructorBinding
Annotations: 
  ProductVersion: 5.0.2
  Relational:MaxIdentifierLength: 128
  Relational:RelationalModel: Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel
  SqlServer:ValueGenerationStrategy: IdentityColumn

image

@ajcvickers ajcvickers added this to the 6.0.0 milestone Jan 29, 2021
@smitpatel smitpatel added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed area-query labels Aug 26, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Aug 27, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
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.

4 participants