entity-framework-corexaf

ModelBuilder HasNoKey not supported for XAF EntityFrameworkCore NonPersistent object?


I have the following class (simplified)

DomainComponent]
[VisibleInReports(true)]
public class SpTestResult  
{
    public string Name {get;set;}
} 

And in OnModelCreating I have

modelBuilder.Entity<SpTestResult>().HasNoKey().ToTable(null); ;

The following code

public static EFCoreObjectSpaceProvider GetEfCoreObjectSpaceProvider()
    {
        var connectionString = GetConnectionString();
        var osp = new EFCoreObjectSpaceProvider(typeof(MyDbContext),
            (builder, _) => builder.UseSqlServer(connectionString));
        return osp;
    }

Throws 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(Type contextType, ServiceCollection services, DbContextOptions dbContextOptions, ITypesInfo typesInfo, EFCoreTypeInfoSource typeInfoSource, String connectionString, EFCoreDatabaseProviderHandler databaseProviderHandler)
   at DevExpress.ExpressApp.EFCore.EFCoreObjectSpaceProvider..ctor(Type contextType, ITypesInfo typesInfo, EFCoreTypeInfoSource typeInfoSource, String connectionString, EFCoreDatabaseProviderHandler databaseProviderHandler)
   at DevExpress.ExpressApp.EFCore.EFCoreObjectSpaceProvider..ctor(Type contextType, EFCoreDatabaseProviderHandler databaseProviderHandler)
   at MyApp.Module.Functions.DataHelpers.GetEfCoreObjectSpaceProvider() in  

The simple work around is to include a primary key in the business object and remove the HasNoKey property in OnModelCreating.

I am wondering if it is necessary to have a primary key.


Solution

  • Yes, if you are going to Xaf approaches like ObjectSpace or show objects in grid etc. Xaf needs a unique Key property on all classes.