Base solution for your next web application
Open Closed

Bug when add new Entity "Cannot create a DbSet for 'ChatMessage' because this type is not included in the model for the context." #6226


User avatar
0
trunglq created

I found that when I add an entity with name start in "C" character, there is an error in Chat function.

public virtual DbSet<Template> Templates { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<CustomField> CustomFields { get; set; }
public virtual DbSet<VatInvoice> VatInvoices { get; set; }

When I add entity start with "C" in name, there is an error show up

I follow the errors and debug to UserFriendsCache class and found the exception "Cannot create a DbSet for 'ChatMessage' because this type is not included in the model for the context."

The stack trace of error is:

   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Expression()
   at System.Linq.Queryable.GetSourceExpression[TSource](IEnumerable`1 source)
   at System.Linq.Queryable.GroupJoin[TOuter,TInner,TKey,TResult](IQueryable`1 outer, IEnumerable`1 inner, Expression`1 outerKeySelector, Expression`1 innerKeySelector, Expression`1 resultSelector)
   at CloudFramework.Friendships.Cache.UserFriendsCache.GetUserFriendsCacheItemInternal(UserIdentifier userIdentifier) in D:\Documents\Visual Studio 2017\Projects\ITT\CloudInvoice\src\CloudFramework.Core\Friendships\Cache\UserFriendsCache.cs:line 183
   at Castle.Proxies.Invocations.UserFriendsCache_GetUserFriendsCacheItemInternal.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Github\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 68
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.UserFriendsCacheProxy.GetUserFriendsCacheItemInternal(UserIdentifier userIdentifier)
   at CloudFramework.Friendships.Cache.UserFriendsCache.&lt;&gt;c__DisplayClass8_0.&lt;GetCacheItem&gt;b__0(String f) in D:\Documents\Visual Studio 2017\Projects\ITT\CloudInvoice\src\CloudFramework.Core\Friendships\Cache\UserFriendsCache.cs:line 48
   at Abp.Runtime.Caching.CacheExtensions.&lt;&gt;c__DisplayClass5_0`2.<Get>b__0(String k) in D:\Github\aspnetboilerplate\src\Abp\Runtime\Caching\CacheExtensions.cs:line 40
   at Abp.Runtime.Caching.CacheBase.Get(String key, Func`2 factory) in D:\Github\aspnetboilerplate\src\Abp\Runtime\Caching\CacheBase.cs:line 68
   at Abp.Runtime.Caching.CacheExtensions.Get[TKey,TValue](ICache cache, TKey key, Func`2 factory) in D:\Github\aspnetboilerplate\src\Abp\Runtime\Caching\CacheExtensions.cs:line 40
   at CloudFramework.Friendships.Cache.UserFriendsCache.GetCacheItem(UserIdentifier userIdentifier) in D:\Documents\Visual Studio 2017\Projects\ITT\CloudInvoice\src\CloudFramework.Core\Friendships\Cache\UserFriendsCache.cs:line 46

But when I remove entity Customer and CustomFields, the exception was disappear . I can't understand what the reason for this bug. Please help me.


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you share your Dbcontext class ?

    Thanks,

  • User Avatar
    0
    trunglq created

    This is my DbContext class:

    using Abp.IdentityServer4;
    using Abp.Zero.EntityFrameworkCore;
    using CloudFramework.Authorization.Roles;
    using CloudFramework.Authorization.Users;
    using CloudFramework.Chat;
    using CloudFramework.DynamicFields;
    using CloudFramework.Customers;
    using CloudFramework.Editions;
    using CloudFramework.Friendships;
    using CloudFramework.MultiTenancy;
    using CloudFramework.MultiTenancy.Accounting;
    using CloudFramework.MultiTenancy.Payments;
    using CloudFramework.Storage;
    using CloudFramework.Templates;
    using Microsoft.EntityFrameworkCore;
    
    namespace CloudFramework.EntityFrameworkCore
    {
        public class CloudFrameworkDbContext : AbpZeroDbContext<Tenant, Role, User, CloudFrameworkDbContext>, IAbpPersistedGrantDbContext
        {
            /* Define an IDbSet for each entity of the application */
    
            public virtual DbSet<BinaryObject> BinaryObjects { get; set; }
    
            public virtual DbSet<Friendship> Friendships { get; set; }
    
            public virtual DbSet<ChatMessage> ChatMessages { get; set; }
    
            public virtual DbSet<SubscribableEdition> SubscribableEditions { get; set; }
    
            public virtual DbSet<SubscriptionPayment> SubscriptionPayments { get; set; }
    
            public virtual DbSet<Invoice> Invoices { get; set; }
    
            public virtual DbSet<PersistedGrantEntity> PersistedGrants { get; set; }
    
            //Cloud Invoice Section
            public virtual DbSet<Template> Templates { get; set; }
            
            public virtual DbSet<Customer> Customers { get; set; }
            public virtual DbSet<DynamicField> DynamicFields { get; set; }
    
    
            public CloudFrameworkDbContext(DbContextOptions<CloudFrameworkDbContext> options)
                : base(options)
            {
    
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
    
                modelBuilder.Entity<BinaryObject>(b =>
                {
                    b.HasIndex(e => new { e.TenantId });
                });
    
                modelBuilder.Entity<ChatMessage>(b =>
                {
                    b.HasIndex(e => new { e.TenantId, e.UserId, e.ReadState });
                    b.HasIndex(e => new { e.TenantId, e.TargetUserId, e.ReadState });
                    b.HasIndex(e => new { e.TargetTenantId, e.TargetUserId, e.ReadState });
                    b.HasIndex(e => new { e.TargetTenantId, e.UserId, e.ReadState });
                });
    
                modelBuilder.Entity<Friendship>(b =>
                {
                    b.HasIndex(e => new { e.TenantId, e.UserId });
                    b.HasIndex(e => new { e.TenantId, e.FriendUserId });
                    b.HasIndex(e => new { e.FriendTenantId, e.UserId });
                    b.HasIndex(e => new { e.FriendTenantId, e.FriendUserId });
                });
    
                modelBuilder.Entity<Tenant>(b =>
                {
                    b.HasIndex(e => new { e.SubscriptionEndDateUtc });
                    b.HasIndex(e => new { e.CreationTime });
                });
    
                modelBuilder.Entity<SubscriptionPayment>(b =>
                {
                    b.HasIndex(e => new { e.Status, e.CreationTime });
                    b.HasIndex(e => new { e.PaymentId, e.Gateway });
                });
    
                modelBuilder.Entity<DynamicField>(C =>
                {
                    C.HasIndex(e => new { e.SystemType });
                });
    
                modelBuilder.ConfigurePersistedGrantEntity();
            }
        }
    }
    
    

    I must change CustomField to DynamicField, and comment Customer entity, the system works perfect like charm. This bug is really strange.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @trunglq

    Yes, this is really strange and I couldn't make a connection between ChatMessage entity and the ones you are adding. If you can use your solution, we can close the issue.

  • User Avatar
    0
    maliming created
    Support Team

    @trunglq

    I want to tell you, we found a solution, please see: https://github.com/aspnetzero/aspnet-zero-core/issues/2372#issuecomment-488954566