Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET boilerplate new module's dbcontext recreate abp main tables #6910

Open
manar-m93 opened this issue Mar 13, 2024 · 6 comments
Open

ASP.NET boilerplate new module's dbcontext recreate abp main tables #6910

manar-m93 opened this issue Mar 13, 2024 · 6 comments

Comments

@manar-m93
Copy link

I created an ASP.NET boilerplate multi-tenancy startup template, then created new module in src folder with Application, Core, Entity Framework Core, and Web projects that Main project depends on.

The new module has its own dbcontext. I put new entities in the new module that have navigation properties to User table in the main module. Problem is every time I add new migration to the new module , all the tables of abp are recreated, I want just the new tables to be added with the right foreign keys to the main tables.

ChatUser in the new module:
public class ChatUser : AbpUser{}
The new entity in the new module :
public class Message : CreationAuditedEntity, IMustHaveTenant
{
public string Content { get; set; }
public int TenantId { get; set; }
public long SenderId { get; set; }
[ForeignKey(nameof(SenderId))]
public virtual ChatUser User { get; set; }
}
And the new module's dbContext is:

public class ChatDbContext : AbpZeroDbContext<ChatTenant, ChatRole, ChatUser, ChatDbContext>
{
public DbSet Messages { get; set; }

public ChatDbContext(DbContextOptions<ChatDbContext> options)
       : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    RegisterConfigurations(modelBuilder);
}

}
I don't know where the problem is. I tried to put the dbContext in new module as:
public class ChatDbContext : AbpDbContext
but then an exception happens when adding migration:
Unable to create a 'DbContext' of type ''. The exception 'Unable to determine the relationship represented by navigation 'ChatUser.CreatorUser' of type 'ChatUser'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'

@ismcagdas
Copy link
Member

Because these base entities are defined in AbpZeroDbContext. Have you tried inheriting from DbContext only ?

@manar-m93
Copy link
Author

manar-m93 commented Mar 14, 2024

@ismcagdas yes I tried and then an exception is thrown when adding an migration:
Unable to create a 'DbContext' of type ''. The exception 'Unable to determine the relationship represented by navigation 'ChatUser.CreatorUser' of type 'ChatUser'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'

@manar-m93
Copy link
Author

@ismcagdas using AbpDbContext and DbContext gives the same exception

@ismcagdas
Copy link
Member

This is not basically related to ASP.NET Boilerplate but if you can provide a reproduction project, we can taka a look.

@manar-m93
Copy link
Author

@ismcagdas can I ask what is the best approach, to make central dbContext in main profgram, or to have each module define its dbcontext. With the case of central db context, this will make main project depends on all new modules .. and that's okay but if I want to use entities from main program inside the new modules that's will make the new modules depends on main program and that's not possible with central dbContext .. I am a little confused in this problem

@ismcagdas
Copy link
Member

I haven't tried this approach before. Maybe this sample can help you https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants