OData / OData/AspNetCoreOData

How to create EntitySet for all DBSet<> in DbContext dynamically?

Open
#169 10 comments 2 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since May 21, 2021.

investigated
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

var odataBuilder = new ODataConventionModelBuilder();

When using the AddEntitySet(), my endpoints fail with the error:
System.InvalidOperationException: 'The entity set 'foo' is based on type 'foo' that has no keys defined.'

var t = typeof(foo);
odataBuilder.AddEntitySet(t.Name, odataBuilder.AddEntityType(t));

I am using Ef Core with fluent api to set key and navigation attributes

    [Table(nameof(foo))]
    public partial class foo:
        IEntityTypeConfiguration<foo>
    {
        void IEntityTypeConfiguration<foo>.Configure(EntityTypeBuilder<foo> builder)
        {
            _ = builder.HasKey(x => x.RecKey);
            _ = builder.HasIndex(x => x.Name).IsUnique();
        }
    }

How can I dynamically configure each EntitySet for my DbContext? Explicitly defining each of the hundreds of DbSet<> in my DbContext is both time consuming and error prone.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.