Luluh
Luluh
Explore posts from servers
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
because im getting error when multiple instances are running and quering the db in same time
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
i think the db was trying to create the blocks again because the blocks were not being tracked
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
yees, i fixed this initializing the dbcontext on the page instead reinitialize in every method on the service
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
.
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
i alread fixed that
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
No description
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
nope, this problem is occuring because im trying to insert the palette on another dbcontext that i get the blocks, so the blocks is no more tracked by the dbcontext and the efcore tries to create a new block insteaded connect with the palette
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
still having issues, i think the problem is not related with keys
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
palette and blocks is a many to many relation
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
public class Block
{
public Guid Id { get; set; } = Guid.CreateVersion7();
public string MinecraftId { get; set; }
public int MapId { get; set; }
public string? ImageUrl { get; set; }

public Dictionary<string, string> Properties { get; set; } = new();
public Dictionary<string, string> GeneratorProperties { get; set; } = new();

public List<Palette> Palettes { get; set; } = [];
}

public class Palette
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }

public Guid PlaceholderBlockId { get; set; }
public Block PlaceholderBlock { get; set; }

public List<Block> Blocks { get; set; } = [];

public string UserId { get; set; }
public ApplicationUser User { get; set; }
}

public class ApplicationUser : IdentityUser
{
public List<Palette> Palettes { get; set; } = [];
}
public class Block
{
public Guid Id { get; set; } = Guid.CreateVersion7();
public string MinecraftId { get; set; }
public int MapId { get; set; }
public string? ImageUrl { get; set; }

public Dictionary<string, string> Properties { get; set; } = new();
public Dictionary<string, string> GeneratorProperties { get; set; } = new();

public List<Palette> Palettes { get; set; } = [];
}

public class Palette
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }

public Guid PlaceholderBlockId { get; set; }
public Block PlaceholderBlock { get; set; }

public List<Block> Blocks { get; set; } = [];

public string UserId { get; set; }
public ApplicationUser User { get; set; }
}

public class ApplicationUser : IdentityUser
{
public List<Palette> Palettes { get; set; } = [];
}
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
public class DatabaseContext(DbContextOptions<DatabaseContext> options, IConfiguration config) : IdentityDbContext<ApplicationUser>(options)
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(config["Database:ConnectionString"]);

public DbSet<Block> Blocks { get; set; }
public DbSet<Palette> Palettes { get; set; }

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

modelBuilder.Entity<Block>()
.HasIndex(b => b.MinecraftId)
.IsUnique();

modelBuilder.Entity<Palette>()
.HasMany(p => p.Blocks)
.WithMany(b => b.Palettes)
.UsingEntity(j => j.ToTable("PaletteBlocks"));

modelBuilder.Entity<Palette>()
.HasOne(p => p.PlaceholderBlock)
.WithMany()
.HasForeignKey(p => p.PlaceholderBlockId)
.OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.Palettes)
.WithOne(p => p.User)
.HasForeignKey(p => p.UserId);

modelBuilder.Entity<IdentityUserLogin<string>>(entity =>
{
entity.HasKey(l => new { l.LoginProvider, l.ProviderKey });
});

modelBuilder.Entity<IdentityUserRole<string>>(entity =>
{
entity.HasKey(r => new { r.UserId, r.RoleId });
});

modelBuilder.Entity<IdentityUserToken<string>>(entity =>
{
entity.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });
});
}
}
public class DatabaseContext(DbContextOptions<DatabaseContext> options, IConfiguration config) : IdentityDbContext<ApplicationUser>(options)
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(config["Database:ConnectionString"]);

public DbSet<Block> Blocks { get; set; }
public DbSet<Palette> Palettes { get; set; }

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

modelBuilder.Entity<Block>()
.HasIndex(b => b.MinecraftId)
.IsUnique();

modelBuilder.Entity<Palette>()
.HasMany(p => p.Blocks)
.WithMany(b => b.Palettes)
.UsingEntity(j => j.ToTable("PaletteBlocks"));

modelBuilder.Entity<Palette>()
.HasOne(p => p.PlaceholderBlock)
.WithMany()
.HasForeignKey(p => p.PlaceholderBlockId)
.OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.Palettes)
.WithOne(p => p.User)
.HasForeignKey(p => p.UserId);

modelBuilder.Entity<IdentityUserLogin<string>>(entity =>
{
entity.HasKey(l => new { l.LoginProvider, l.ProviderKey });
});

modelBuilder.Entity<IdentityUserRole<string>>(entity =>
{
entity.HasKey(r => new { r.UserId, r.RoleId });
});

modelBuilder.Entity<IdentityUserToken<string>>(entity =>
{
entity.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });
});
}
}
My DB
85 replies
CC#
Created by Luluh on 10/1/2024 in #help
✅ Problem connecting entities on efcore
im trying to create a new palette on db and connect blocks from another table to the palette, but it looks the db is trying to create another blocks insteaded connect
85 replies