Base solution for your next web application
Open Closed

Second Context Problem #2906


User avatar
0
mpm created

I'm having a few issues getting a second context to a vended app database wired up properly (second context should not use code first). The specific error I am getting when attempting to run 'abp.services.app.vended.getAttributes()' is 'error detail not sent by server', with a response of '{"message":"An error has occurred."}'.

I've looked at the MultiContextDemo app, and think I have things wired up properly, but I am obviously missing something.

CSDataModule

[DependsOn(typeof(AbpZeroEntityFrameworkModule), typeof(CSCoreModule))]
    public class CSDataModule : AbpModule
    {
        public override void PreInitialize()
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<CSDbContext>());
            
            //web.config (or app.config for non-web projects) file should contain a connection string named "Default".
            Configuration.DefaultNameOrConnectionString = "Default";
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            Database.SetInitializer<VendedDbContext>(null);
        }
    }

VendedDbContext

public class VendedDbContext : AbpDbContext
    {
        public VendedDbContext()
            : base("Vended")
        {
            
        }
    }

Attribute Class

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities;

namespace MCMT.CS.Vended
{
   /// <summary>
    /// A class which represents the Attribute table.
    /// </summary>
	[Table("Attribute")]
    public class Attribute : Entity
    {
        [Key]
        public virtual int AttributeID { get; set; }
        public virtual int ResourceTypeID { get; set; }
        public virtual string Name { get; set; }
        public virtual int AttributeTypeID { get; set; }
        public virtual int? SortOrder { get; set; }
        public virtual string DefaultValue { get; set; }
        public virtual byte? ResNameIndex { get; set; }
        public virtual bool IsUnique { get; set; }
        public virtual byte? RepNameIndex { get; set; }
        public virtual string RepNameFormat { get; set; }
        public virtual bool IsCertification { get; set; }
        public virtual bool IsPublic { get; set; }
        public virtual int? SpecialTypeID { get; set; }
        public virtual bool ShowOnTimesheet { get; set; }
        public virtual bool IsSelfServe { get; set; }
        public virtual string CertType { get; set; }
        public virtual int? CertWarnDays { get; set; }
        public virtual int? CertExpireDays { get; set; }
        public virtual string CertExpireValue { get; set; }
        public virtual bool IsContact { get; set; }
        public virtual int? CertValueOperator { get; set; }
        public virtual bool CertIgnoreNull { get; set; }
        public virtual bool CertForbidPunch { get; set; }
    }
}

Vended App Service Interface

using System.Collections.Generic;
using Abp.Application.Services;

namespace MCMT.CS.Vended
{
    public interface IVendedAppService : IApplicationService
    {
        List<Attribute> GetAttributes();
    }
}

Vended App Service

using System.Collections.Generic;
using System.Linq;
using Abp.Auditing;
using Abp.Domain.Repositories;

namespace MCMT.CS.Vended
{
    [DisableAuditing]
    public class VendedAppService : CSAppServiceBase, IVendedAppService
    {
        private readonly IRepository<Attribute> _aRepository;
        
        public VendedAppService(IRepository<Attribute> aRepository)
        {
            _aRepository = aRepository;
        }

        public List<Attribute> GetAttributes()
        {
            var attributes = _aRepository.GetAllList().ToList();
            return attributes;
        }
    }
}

Any assistance would be appreciated.


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

    Hi,

    Your usage seems correct. Can you check Logs.txt file under your Web project. It might contain detailed error message.

    Thanks.

  • User Avatar
    0
    mpm created

    This turned out to be a dumb mistake on my part. I forgot to define an IDbSet for each entity in my second database context. :roll:

    Working flawlessly now!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks for the feedback :)