Search code examples
aspnetboilerplateabp-framework

Unable to configure relationship for Auditable Entity using Fluent API


I am using asp.net boilerplate template for my project.

Looks like it preferred to use data annotation instead of fluent API.

I like to use Fluent API since it's very clean.

But when I tried to use fluent API for my entity it worked fine, but as soon as I started using an entity from abp framework like Auditable entity, I was not able to add migration as it was asking me to configure a relationship for CreatorUser, which is defined in Auditable entity and mapped using data annotation.

enter image description here

I was still able to configure CreatorUser, but that ended up in dropping FK for CreatorUser from another table and re-creating it.

This is how my entity is configured. let me know if anyone is able to use fluent API in aspnetboiler plate.

    public class ItemGroup : FullAuditedEntity<int,AbpUser<User>>
        {
            public string Name { get; set; }
            public string Description { get; set; }
    
            //[ForeignKey("Id")]
            public int? ParentItemGroupId { get; set; }
            public virtual ItemGroup ParentItemGroup { get; set; }
            public virtual ICollection<ItemGroup> ItemGroups { get; set; }
    
            //public virtual AbpUser<User> CreatorUser { get; set; }
            //To-Do : Need to add parent itemgroup id, but self referencing is not supported in abpframework/EFCore, need to solve this and add it later.
        }
    
        public class ItemGroupEntityConfiguration : IEntityTypeConfiguration<ItemGroup>
        {
    
            public void Configure(EntityTypeBuilder<ItemGroup> builder)
            {
                builder.ToTable("ItemGroups");
                builder.HasOne(x => x.ParentItemGroup).WithMany(x => x.ItemGroups).HasForeignKey(b => b.ParentItemGroupId);
                builder.HasOne(x => x.CreatorUser).WithMany().HasForeignKey(i => i.CreatorUserId);
            }
        }

Solution

  • Finally I found solution. It was very silly error I made. I was not calling base.OnModelCreating so it was not able to configure relationship. after this I was able to run migration.

    enter image description here

    Also like jonas mentioned above, I passed User instead of AbpUser. Infact I don't need to pass User as well , since I don't need User navigation properties so its find without user.

    enter image description here