Skip to content

C#: type references resolve to a same-named property (DbSet<T>) instead of the type declaration #3034

Open

Description

@brobl2008

Summary

In C#, a reference to a type resolves to a same-named property declared in another class rather than to the type itself. DbSet<T> properties in an Entity Framework context make this the common case: public DbSet<Widget> Widget { get; set; } declares a property named Widget, and from then on every parameter_type / generic_arg / return_type reference to the class Widget lands on that property node instead of the class.

Separately, new Widget { ... } in a different file resolves to a bare sourceless stub rather than to the class, and db.Widget member access emits no edge at all.

Reproduces on 0.9.49 with --code-only --no-cluster, two files, no dependencies.

Reproduction

Ctx.cs:

using Microsoft.EntityFrameworkCore;

namespace App.Data;

public class Widget
{
    public int Id { get; set; }
}

public class AppContext : DbContext
{
    public DbSet<Widget> Widget { get; set; }   // property named the same as the class
}

Service.cs:

using System.Collections.Generic;
using System.Linq;
using App.Data;

namespace App.Services;

public static class WidgetAdapter
{
    public static string Describe(Widget widget) => widget.Id.ToString();      // parameter_type
    public static int CountAll(List<Widget> widgets) => widgets.Count;         // generic_arg
}

public class WidgetService(AppContext db)
{
    public string First()
    {
        var name = db.Widget.First().Id.ToString();    // member access -- no edge emitted
        return name;
    }

    public Widget Make() => new Widget { Id = 1 };     // return_type + object creation
}
graphify extract . --code-only --no-cluster --out ./out

Actual

Every edge that should point at the class Widget (Ctx.cs:L5, id ctx_app_data_widget) points at the property (Ctx.cs:L12, id ctx_app_data_appcontext_widget) instead:

Ctx.cs       --contains-->                 Widget @ Ctx.cs:L5    id=ctx_app_data_widget
AppContext   --defines/field-->            Widget @ Ctx.cs:L12   id=ctx_app_data_appcontext_widget
AppContext   --references/generic_arg-->   Widget @ Ctx.cs:L12   id=ctx_app_data_appcontext_widget
.Describe()  --references/parameter_type-->Widget @ Ctx.cs:L12   id=ctx_app_data_appcontext_widget
.CountAll()  --references/generic_arg-->   Widget @ Ctx.cs:L12   id=ctx_app_data_appcontext_widget
.Make()      --references/return_type-->   Widget @ Ctx.cs:L12   id=ctx_app_data_appcontext_widget
.Make()      --calls/call-->               Widget @ (stub, no source_file)  id=widget

The class node ends up with exactly one inbound edge — the contains from its own file. So graphify affected ctx_app_data_widget reports nothing, while the property node accumulates references that are not about the property.

Note AppContext --references/generic_arg--> Widget@L12 is wrong on its own terms: the generic argument of DbSet<Widget> is the class, not the property being declared.

Expected

parameter_type, generic_arg and return_type references to Widget resolve to ctx_app_data_widget (the class). A type-position reference should not bind to a property/field, since a property name is not legal in a type position. new Widget { ... } should resolve to the same class node rather than minting a sourceless stub.

db.Widget member access emitting no edge is arguably by design (the property node exists and could carry it), but it means "what code reads this table" is unanswerable from the C# graph — happy to split that into its own issue if you'd prefer.

Why it matters

Measured on a private ~90k-node C# graph, comparing affected against git grep -w on production files with every miss adjudicated by hand:

Asking about Recall
Interface consumers 96-100%
Message-bus contract types (generic-only API surface) high, better than grep
EF entities ~56%
Enums ~56%

Interfaces are near-perfect, so resolution works well in general — it is specifically types that share a name with a property/field in the corpus that degrade, and in an EF codebase that is every entity. I have not proven the repro above is the sole cause of the 56% (in that graph the entity and the context live in different projects, and there the class node does retain some inbound edges), so treat the repro as one confirmed instance rather than a complete explanation.

The graph is of a private codebase, so type names are elided throughout — every shape referenced here is reproduced by the two files above.

Workaround we are using meanwhile: resolve a name to every node carrying its norm_label, filter to those declared in a .cs file, and union the results — including the sourceless stubs at depth 1, which recovers our entity recall from 33% to 56%.

Context: same codebase as #2829 (primary constructors) and #2911 (generic arguments), both of which landed and measurably helped — #2911 in particular made an entirely-generic message-bus API surface traceable end to end. Thanks for both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions