Open Closed

Cannot use table 'ProductOneColumn' for entity type 'ProductOneColumn' since it is being used for entity type 'ProductOneColumn' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'ProductOneColumn' on the primar #4698


User avatar
0
Tisham.Ahuja created

ABP Framework version: v5.3.3

UI type: Blazor

DB provider: EF Core

Tiered (MVC) or Identity Server Separated (Angular): Separated Identity server

Exception message and stack trace: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

Steps to reproduce the issue:" below are the steps to reproduce error

• I have an entity in ModuleA as below: public class ProductOneColumn { [Key] public int Id { get; set; } public string ProductCode { get; set; } }

• Added DB set for this in ModuleADbContext public DbSet<ProductOneColumn> ProductOneColumn { get; set; }

• Below is the code in ModuleADbContextModelCreatingExtensions builder.Entity<ProductOneColumn>(b => { b.ToTable("ProductOneColumn", ProductConstants.DbSchema); b.ConfigureByConvention(); b.Property(x => x.ProductCode).HasMaxLength(8); });

• I have exactly the same entity in ModuleB as well (This entity will also use same DB table being used by ProductOneColumn entity in ModuleA): public class ProductOneColumn { [Key] public int Id { get; set; } public string ProductCode { get; set; } }

• Added DB set for this in ModuleBDbContext public DbSet<ProductOneColumn> ProductOneColumn { get; set; }

• Below is the code in ModuleBDbContextModelCreatingExtensions

builder.Entity<ProductOneColumn>(b => { b.ToTable("ProductOneColumn", FedExConstants.DbSchema, t => t.ExcludeFromMigrations()); b.ConfigureByConvention(); b.Property(x => x.ProductCode).HasMaxLength(8); });

Note: Two different entities in both the modules are trying to access the same table and Only difference between code in both the modules is that in ModuleBDbContextModelCreatingExtensions I have added b.ToTable("ProductOneColumn", FedExConstants.DbSchema, t => t.ExcludeFromMigrations()); instead of b.ToTable("ProductOneColumn", ProductConstants.DbSchema);

• In AppDBContext I have 2 method calls (this code was automatically added when I added modules) builder.ConfigureModuleA(); builder.ConfigureModuleB();

Now when I run API and click on authorize, I get below error: InvalidOperationException: Cannot use table 'ProductOneColumn' for entity type 'ProductOneColumn' since it is being used for entity type 'ProductOneColumn' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'ProductOneColumn' on the primary key properties and pointing to the primary key on another entity type mapped to 'ProductOneColumn'.

• If I comment out builder.ConfigureModuleB(); then It works. However I am not aware of impact of commenting out this line of code.


4 Answer(s)
  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    hi

    Please share these projects, liming.ma@volosoft.com

    I will check it on my local

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    ** (1.1G) is too large**

    Can you run abp clean command in your project and delete all the node_moduels?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    This is a problem with EF Core, Here is my test code with pure EF Core.

    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Design;
    
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=WebApplication1;Trusted_Connection=True;MultipleActiveResultSets=true"));
    
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class Employee2
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<Employee>(b =>
            {
                b.ToTable("Employee", schema: null);
                b.Property(x => x.Name).IsRequired().HasMaxLength(100);
            });
    
            builder.Entity<Employee2>(b =>
            {
                b.ToTable("Employee", schema: null);
                b.Property(x => x.Name).IsRequired().HasMaxLength(100);
            });
    
            base.OnModelCreating(builder);
        }
    }
    
    public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=WebApplication1;Trusted_Connection=True;MultipleActiveResultSets=true");
    
            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }
    
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15