0

I have the following DbContext in a .net5 winforms EF XAF 21.2.5 standard security application created with the wizard and it runs OK.

[TypesInfoInitializer(typeof(DXApplication42ContextInitializer))]
public class DXApplication42EFCoreDbContext : DbContext {
    public DXApplication42EFCoreDbContext(DbContextOptions<DXApplication42EFCoreDbContext> options) : base(options) {
    }
    public DbSet<ModuleInfo> ModulesInfo { get; set; }
    public DbSet<ModelDifference> ModelDifferences { get; set; }
    public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; }
    public DbSet<PermissionPolicyRole> Roles { get; set; }
    public DbSet<DXApplication42.Module.BusinessObjects.ApplicationUser> Users { get; set; }
    public DbSet<DXApplication42.Module.BusinessObjects.ApplicationUserLoginInfo> UserLoginInfos { get; set; }
    public DbSet<ReportDataV2> ReportDataV2 { get; set; }

    //public DbSet<DtoNum> TempNums { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<DXApplication42.Module.BusinessObjects.ApplicationUserLoginInfo>(b => {
            b.HasIndex(nameof(DevExpress.ExpressApp.Security.ISecurityUserLoginInfo.LoginProviderName), nameof(DevExpress.ExpressApp.Security.ISecurityUserLoginInfo.ProviderUserKey)).IsUnique();
        });

        //modelBuilder.Entity<DtoNum>().HasNoKey();
    }
}
public class DtoNum
{
    public int Id { get; set; }
}

However if I uncomment the 2 commented lines I get an error

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=DevExpress.ExpressApp.EFCore.v21.2
  StackTrace:
   at DevExpress.ExpressApp.EFCore.EFCoreTypeInfoSource.SetupKeyMembers(TypeInfo typeInfo)
   at DevExpress.ExpressApp.EFCore.EFCoreTypeInfoSource.RegisterEntity(Type type)
   at DevExpress.ExpressApp.EFCore.TypeInfoSourceHelper.InitTypeInfoSource(EFCoreObjectSpaceProvider objectSpaceProvider)
   at DevExpress.ExpressApp.EFCore.EFCoreObjectSpaceProvider..ctor(IDbContextFactory`1 dbContextFactory, ITypesInfo typesInfo)
   at DevExpress.EntityFrameworkCore.Security.SecuredEFCoreObjectSpaceProvider..ctor(ISelectDataSecurityProvider selectDataSecurityProvider, IDbContextFactory`1 dbContextFactory, ITypesInfo typesInfo)
   at DXApplication42.Win.DXApplication42WindowsFormsApplication.CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) in C:\Users\kirst\source\repos\DXApplication42\DXApplication42.Win\WinApplication.cs:line 34

Shown as

break point

Looking inside EFCoreTypeInfoSources I see

private void SetupKeyMembers(TypeInfo typeInfo) {
    IKey key = EntityTypes[typeInfo.FullName].FindPrimaryKey();
    typeInfo.KeyMember = null;
    foreach(IProperty property in key.Properties) {
        typeInfo.AddKeyMember(typeInfo.FindMember(property.Name));
    }
}

I want to use the non table based DbSet as explained here.

I posted the code to GitHub

Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

0

I think the problem is that not all entities have a key so FindPrimaryKey would return null causing SetupKeyMembers to fail.

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • I have asked if there is an alternative to HasNoKey at https://stackoverflow.com/questions/71029023/is-there-another-way-to-denote-a-dbset-as-having-no-table-other-than-using-hasno – Kirsten Feb 08 '22 at 05:36