Open Closed

Getting Abpuser data based on role which is another model and another database i need to access that data into my another model #4516


User avatar
0
isteer created

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: 7.0.2 latest
  • UI type: MVC
  • DB provider: EF Core / MongoDB
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

Hey abp team As we know when we create over a project we will get the identity model in that we have a user, and role management this is in my main database and I have another one model where I have the Candidate table which is in another database. in roles, i have a practitioner and i want to assign Practitioner to the Candidates

as we can see in the screenshot I have two models with a separate database modelA has Candidates and Mylearning2 has another one database and tables which ABP provide. I want to Access this Mylearning2 database tables on my ModelA model


17 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can set the connection string. See: https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations#change-the-connection-strings-section

  • User Avatar
    0
    isteer created

    Hi,

    You can set the connection string. See: https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations#change-the-connection-strings-section

    Hey, Thanks for replying.

    I have gone through this documentation but in this documentation, they are configuring multiple databases in one module. But I have two modules and I Need the data of the Base module for My other Module I have MY multiple Connection String and dBContext. But I am not able to the list of user whose Role Is Teacher in my Sub Module......

    <br> And as we Can See if I am Adding the Date Of Birth in Configure Extra property It's Showing DD-MM-YY-HH-MM-AM I need only DD-MM-YYYY can anyone suggest how to configure that....

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    they are configuring multiple databases in one module

    The database is separated, and each module can have its own database, by configuring the connection string, a module can access the b database

    See: https://docs.abp.io/en/abp/latest/Connection-Strings

    Or you can use the C# API client proxies, This way you can access the API, but not directly read the database in the module

    And as we Can See if I am Adding the Date Of Birth in Configure Extra property It's Showing DD-MM-YY-HH-MM-AM I need only DD-MM-YYYY can anyone suggest how to configure that....

    You can add DisplayFormatAttribute to the extra property:

    ObjectExtensionManager.Instance.Modules()
    .ConfigureIdentity(identity =>
    {
        identity.ConfigureUser(user =>
        {
            user.AddOrUpdateProperty<DateTime>(
                "Birthday",
                property =>
                {
                    var format = new DisplayFormatAttribute()
                    {
                        DataFormatString = "dd/MM/yyyy"
                    };
    
                    property.Attributes.Add(format);
    
                }
            );
        });
    });
    
  • User Avatar
    0
    isteer created

    they are configuring multiple databases in one module

    The database is separated, and each module can have its own database, by configuring the connection string, a module can access the b database

    See: https://docs.abp.io/en/abp/latest/Connection-Strings

    Or you can use the C# API client proxies, This way you can access the API, but not directly read the database in the module

    And as we Can See if I am Adding the Date Of Birth in Configure Extra property It's Showing DD-MM-YY-HH-MM-AM I need only DD-MM-YYYY can anyone suggest how to configure that....

    You can add DisplayFormatAttribute to the extra property:

    ObjectExtensionManager.Instance.Modules() 
    .ConfigureIdentity(identity => 
    { 
        identity.ConfigureUser(user => 
        { 
            user.AddOrUpdateProperty<DateTime>( 
                "Birthday", 
                property => 
                { 
                    var format = new DisplayFormatAttribute() 
                    { 
                        DataFormatString = "dd/MM/yyyy" 
                    }; 
     
                    property.Attributes.Add(format); 
     
                } 
            ); 
        }); 
    }); 
    

    I tried that it's not working it's showing the same and I need a solution As we know when we create a project we will get the identity model in that we have a user, and role management this is in my main database and I have another model where I have the Candidate table which is in another database. in roles, I have a practitioner and I want to assign Practitioner to the Candidates please help me with this I followed that document but it's not working

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I need a solution As we know when we create a project we will get the identity model in that we have a user, and role management this is in my main database and I have another model where I have the Candidate table which is in another database. in roles, I have a practitioner and I want to assign Practitioner to the Candidates please help me with this I followed that document but it's not working

    This is no document for this because this is just your use case.

    I think your question is how to access the main database(roles, users) in a module. I made an example for you: https://github.com/realLiangshiwei/Qa4516

    • ModelA can access the roles&users by using the repository

    • The ModelA requires the identity(pro) domain and entityframeworkcore package.

    • The ModelA database has no users and roles, it is from the Mainapp database.
    • Specify the connection string for the identity module.
    • Pre-built application modules define constants for the connection string names. For example, the IdentityServer module defines a ConnectionStringName constant in the AbpIdentityServerDbProperties class (located in the Volo.Abp.IdentityServer namespace). Other modules similarly define constants, so you can investigate the connection string name

    And as we Can See if I am Adding the Date Of Birth in Configure Extra property It's Showing DD-MM-YY-HH-MM-AM I need only DD-MM-YYYY can anyone suggest how to configure that....

    Can you share the full steps to reproduce the problem? thanks.

  • User Avatar
    0
    isteer created

    @liangshiwei thanks for Your Valuable Replay Actually, four tables in one database I want to create the one is to one relationship between their tables each table is interconnected to each other if I delete one record of one table It should delete the respected records of another table i i want to generate the crud page for all this table

    and regarding that Data time

      OneTimeRunner.Run(() =>
            {
                ObjectExtensionManager.Instance.Modules()
                    .ConfigureIdentity(identity =>
                    {
                        identity.ConfigureUser(user =>
                        {
                            user.AddOrUpdateProperty<DateTime>("DateOfBirth",
    
                                property =>
                                {
    
                                    var format = new DisplayFormatAttribute()
                                    {
                                        DataFormatString = "dd/MM/yyyy"
                                    };
    
                                    property.Attributes.Add(format);
                                    //validation rules
                                    property.Attributes.Add(new RequiredAttribute());
                                    property.DisplayName = new FixedLocalizableString("Date Of Birth");
    
                                    property.DefaultValueFactory = () => DateTime.Now;
                                    //...other configurations for this property
                                }
                            );
                        });
                    });
            });
        }  
    

    i Have added this code again its showing same

    Can you provide solution for this please Actually, four tables in one database I want to create the one is to one relationship between their tables each table is interconnected to each other if I delete one record of one table It should delete the respected records of another table i i want to generate the crud page for all this table

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    and regarding that Data time

    You can try this:

    user.AddOrUpdateProperty<DateTime>("DateOfBirth", property =>
        {
            property.Attributes.Add(new DataTypeAttribute(DataType.Date));
    
            //validation rules
            property.Attributes.Add(new RequiredAttribute());
            property.DisplayName = new FixedLocalizableString("Date Of Birth");
    
            property.DefaultValueFactory = () => DateTime.Now;
            //...other configurations for this property
        }
    );
    

    Can you provide solution for this please Actually, four tables in one database I want to create the one is to one relationship between their tables each table is interconnected to each other if I delete one record of one table It should delete the respected records of another table i i want to generate the crud page for all this table

    You should make sure that the modules' tables are all in the same database. once you create relationships for tables correct, they are cascaded deletes: https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete

  • User Avatar
    0
    isteer created

    @liangshiwei Thanks For your replay Can you provide a solution for this, please? Actually, 1 four tables in one database I want to create the one is to one relationship between their tables each table is interconnected to each other if I delete one record of one table It should delete the respected records of another table I want to generate the crud page for all this table. want each and every table to connect one on one with the Table of Candidates. this is the main table I want all the tables to connect one on one to the Candidates' table. In the ABP suite, there is the only option of 1-N and N-N but how can I do 1-1 please help me with this... Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    See: https://docs.abp.io/en/commercial/latest/abp-suite/generating-crud-page#navigation-properties

    ABP Suite allows you to create a navigation property for 1-to-many (1:N) and many-to-many (N:N) relationships.

    You can't do it with the ABP suite, but it's easy to do yourself:

    public class MyEntity1 : AggregateRoot<Guid>
    {
        public MyEntity1(Guid id)
        {
            Id = id;
        }
    
        public string Name { get; set; }
    
        public Guid MyEntity2Id { get; set; }
        public virtual MyEntity2 MyEntity2 { get; set; }
    }
    
    public class MyEntity2 : AggregateRoot<Guid>
    {
        public MyEntity2(Guid id)
        {
            Id = id;
        }
    
    
        public string Name { get; set; }
    
        public Guid MyEntity1Id { get; set; }
        public virtual MyEntity1 MyEntity1 { get; set; }
    }
    
    
    public DbSet<MyEntity1> MyEntityTest { get; set; }
    public DbSet<MyEntity2> MyEntityTest2 { get; set; }
    
    builder.Entity<MyEntity1>(b =>
    {
        b.HasOne(x => x.MyEntity2)
            .WithOne(x => x.MyEntity1)
            .HasForeignKey<MyEntity2>(x => x.MyEntity1Id)
            .OnDelete(DeleteBehavior.Cascade);
    });
    
    public class TestAppService : QaAppService
    {
        private readonly IRepository<MyEntity1> _myEntity1Repository;
    
        public TestAppService(IRepository<MyEntity1> myEntity1Repository)
        {
            _myEntity1Repository = myEntity1Repository;
        }
    
        public async Task CreateAsync()
        {
            var entity1Id = GuidGenerator.Create();
            var entity2Id = GuidGenerator.Create();
            var entity = new MyEntity1(entity1Id)
            {
                Name = "test",
                MyEntity2Id = entity2Id,
                MyEntity2 = new MyEntity2(entity2Id)
                {
                    MyEntity1Id = entity1Id,
                    Name = "test2"
                }
            };
    
            await _myEntity1Repository.InsertAsync(entity);
        }
    
        public async Task DeleteAsync()
        {
            var entity = await (await _myEntity1Repository.GetQueryableAsync()).Include(x=>x.MyEntity2).FirstOrDefaultAsync();
    
            await _myEntity1Repository.DeleteAsync(entity);
        }
    }
    

    Deleting a Myentity1 will also delete the Myentity2, but, of course, not the other way around

    Note: Only deleting the principal entity will cascade delete the dependent entity. Vice-versa is not possible.

    If you want to delete any entity, the records of other tables will be deleted. You need to delete them manually.

  • User Avatar
    0
    isteer created

    Hi,

    See: https://docs.abp.io/en/commercial/latest/abp-suite/generating-crud-page#navigation-properties

    ABP Suite allows you to create a navigation property for 1-to-many (1:N) and many-to-many (N:N) relationships.

    You can't do it with the ABP suite, but it's easy to do yourself:

    public class MyEntity1 : AggregateRoot<Guid> 
    { 
        public MyEntity1(Guid id) 
        { 
            Id = id; 
        } 
     
        public string Name { get; set; } 
     
        public Guid MyEntity2Id { get; set; } 
        public virtual MyEntity2 MyEntity2 { get; set; } 
    } 
     
    public class MyEntity2 : AggregateRoot<Guid> 
    { 
        public MyEntity2(Guid id) 
        { 
            Id = id; 
        } 
     
     
        public string Name { get; set; } 
     
        public Guid MyEntity1Id { get; set; } 
        public virtual MyEntity1 MyEntity1 { get; set; } 
    } 
    
     
    public DbSet<MyEntity1> MyEntityTest { get; set; } 
    public DbSet<MyEntity2> MyEntityTest2 { get; set; } 
     
    builder.Entity<MyEntity1>(b => 
    { 
        b.HasOne(x => x.MyEntity2) 
            .WithOne(x => x.MyEntity1) 
            .HasForeignKey<MyEntity2>(x => x.MyEntity1Id) 
            .OnDelete(DeleteBehavior.Cascade); 
    }); 
    
    public class TestAppService : QaAppService 
    { 
        private readonly IRepository<MyEntity1> _myEntity1Repository; 
     
        public TestAppService(IRepository<MyEntity1> myEntity1Repository) 
        { 
            _myEntity1Repository = myEntity1Repository; 
        } 
     
        public async Task CreateAsync() 
        { 
            var entity1Id = GuidGenerator.Create(); 
            var entity2Id = GuidGenerator.Create(); 
            var entity = new MyEntity1(entity1Id) 
            { 
                Name = "test", 
                MyEntity2Id = entity2Id, 
                MyEntity2 = new MyEntity2(entity2Id) 
                { 
                    MyEntity1Id = entity1Id, 
                    Name = "test2" 
                } 
            }; 
     
            await _myEntity1Repository.InsertAsync(entity); 
        } 
     
        public async Task DeleteAsync() 
        { 
            var entity = await (await _myEntity1Repository.GetQueryableAsync()).Include(x=>x.MyEntity2).FirstOrDefaultAsync(); 
     
            await _myEntity1Repository.DeleteAsync(entity); 
        } 
    } 
    

    Deleting a Myentity1 will also delete the Myentity2, but, of course, not the other way around

    Note: Only deleting the principal entity will cascade delete the dependent entity. Vice-versa is not possible.

    If you want to delete any entity, the records of other tables will be deleted. You need to delete them manually.

    @liangshiwei thanks for your replay

    Actually, when I am running my application I am getting this exception please help me with This

    The code is running fine with my other team but when I am trying to run the application i am getting this error

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can check it: https://support.abp.io/QA/Questions/4483/After-login-It-is-not-redirecting-to-swagger-page#answer-0dac1928-15ab-ea7a-ad93-3a093a97493f

  • User Avatar
    0
    isteer created

    Hi,

    You can check it: https://support.abp.io/QA/Questions/4483/After-login-It-is-not-redirecting-to-swagger-page#answer-0dac1928-15ab-ea7a-ad93-3a093a97493f

    but I am running this application in local system my laptop

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The same applies to local

  • User Avatar
    0
    isteer created

    @liangshiwei thanks for your replay through That API Approach, I am able to create API and get the user Data through API Know I have One Candidate Table and I have created a CRUD page for that. The Data I am getting I want to show in the Candidate Table in the form of a Dropdown And the selected Value of the Dropdown I want to store in the Database are in Candidate Table Can you help me with this thing... can you update this thing in https://github.com/realLiangshiwei/Qa4516 this project only.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think the main question of this ticket is solved, I close the question. please create a new ,thanks.

  • User Avatar
    0
    isteer created

    @liangshiwei thanks for your replay through That API Approach, I am able to create API and get the user Data through API Know I have One Candidate Table and I have created a CRUD page for that. The Data I am getting I want to show in the Candidate Table in the form of a Dropdown And the selected Value of the Dropdown I want to store in the Database are in Candidate Table Can you help me with this thing... can you update this thing in https://github.com/realLiangshiwei/Qa4516 this project only.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Duplicate of https://support.abp.io/QA/Questions/4547/Navigation-Property-for-IdentityUser-in-other-database-table#answer-3a098ea7-e930-14aa-4e43-430a3d73ad1d

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09