question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some columns with int datatype are incorrectly created as counter column

See original GitHub issue

Hello,

currently I try to recreate an Access MDB database based on the scaffolded project. When I run my initial migration some columns which are mapped to an integer property but without explicitly assigned datatype are created incorrectly as counter columns. Some other columns with similar definition/configuration are created properly as int.

Currently I believe the incorrect behavior comes up when the column is referencing some other column as foreign key. In the example below, none of the columns should get the counter datatype.

I’m using EFCore 3.1.21 on net472 together with EntityFrameworkCore.Jet.OleDb 3.1.0

public partial class AmElCommodityMaterials
{
    public int? Ix4CommodityId { get; set; }
    public int? Ix4MaterialId { get; set; }
    public short? Ix2MaterialVc { get; set; }
    public int? Ix4MaterialFunctionId { get; set; }

    public virtual AmElCommodityCatalog Ix4Commodity { get; set; }
    public virtual AmElMaterialData Ix4Material { get; set; }
    public virtual AmElMaterialFunction Ix4MaterialFunction { get; set; }
}

// From ModelBuilder
modelBuilder.Entity<AmElCommodityMaterials>(entity =>
{
    entity.HasKey(e => new { e.Ix4CommodityId, e.Ix4MaterialFunctionId })
        .HasName("pk_ETCommodityMaterials");

    entity.ToTable("AM_EL_CommodityMaterials");

    entity.HasIndex(e => e.Ix4CommodityId)
        .HasName("fk_AM_EL_CommodityMaterials_AM_EL_CommodityCatalog");

    entity.HasIndex(e => e.Ix4MaterialFunctionId)
        .HasName("fk_AM_EL_CommodityMaterials_AM_EL_MaterialFunction");

    entity.HasIndex(e => e.Ix4MaterialId)
        .HasName("fk_AM_EL_CommodityMaterials_AM_EL_MaterialData");

    entity.Property(e => e.Ix4CommodityId).HasColumnName("Ix4_CommodityID");

    entity.Property(e => e.Ix4MaterialFunctionId).HasColumnName("Ix4_MaterialFunctionID");

    entity.Property(e => e.Ix2MaterialVc).HasColumnName("Ix2_Material_VC");

    entity.Property(e => e.Ix4MaterialId).HasColumnName("Ix4_MaterialID");

    entity.HasOne(d => d.Ix4Commodity)
        .WithMany(p => p.AmElCommodityMaterials)
        .HasForeignKey(d => d.Ix4CommodityId)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("fk_AM_EL_CommodityMaterials_AM_EL_CommodityCatalog");

    entity.HasOne(d => d.Ix4MaterialFunction)
        .WithMany(p => p.AmElCommodityMaterials)
        .HasForeignKey(d => d.Ix4MaterialFunctionId)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("fk_AM_EL_CommodityMaterials_AM_EL_MaterialFunction");

    entity.HasOne(d => d.Ix4Material)
        .WithMany(p => p.AmElCommodityMaterials)
        .HasForeignKey(d => d.Ix4MaterialId)
        .HasConstraintName("fk_AM_EL_CommodityMaterials_AM_EL_MaterialData");
});

// From Migration
migrationBuilder.CreateTable(
    name: "AM_EL_CommodityMaterials",
    columns: table => new
    {
        Ix4_CommodityID = table.Column<int>(nullable: false),
        Ix4_MaterialFunctionID = table.Column<int>(nullable: false),
        Ix4_MaterialID = table.Column<int>(nullable: true),
        Ix2_Material_VC = table.Column<short>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("pk_ETCommodityMaterials", x => new { x.Ix4_CommodityID, x.Ix4_MaterialFunctionID });
        table.ForeignKey(
            name: "fk_AM_EL_CommodityMaterials_AM_EL_CommodityCatalog",
            column: x => x.Ix4_CommodityID,
            principalTable: "AM_EL_CommodityCatalog",
            principalColumn: "Ix4_CommodityID",
            onDelete: ReferentialAction.Restrict);
        table.ForeignKey(
            name: "fk_AM_EL_CommodityMaterials_AM_EL_MaterialFunction",
            column: x => x.Ix4_MaterialFunctionID,
            principalTable: "AM_EL_MaterialFunction",
            principalColumn: "Ix4_MaterialFunctionID",
            onDelete: ReferentialAction.Restrict);
        table.ForeignKey(
            name: "fk_AM_EL_CommodityMaterials_AM_EL_MaterialData",
            column: x => x.Ix4_MaterialID,
            principalTable: "AM_EL_MaterialData",
            principalColumn: "Ix4_MaterialID",
            onDelete: ReferentialAction.Restrict);
    });
CREATE TABLE `AM_EL_CommodityMaterials` (
    `Ix4_CommodityID` counter NOT NULL,
    `Ix4_MaterialFunctionID` counter NOT NULL,
    `Ix4_MaterialID` counter NULL,
    `Ix2_Material_VC` smallint NULL,
    CONSTRAINT `pk_ETCommodityMaterials` PRIMARY KEY (`Ix4_CommodityID`, `Ix4_MaterialFunctionID`),
    CONSTRAINT `fk_AM_EL_CommodityMaterials_AM_EL_CommodityCatalog` FOREIGN KEY (`Ix4_CommodityID`) REFERENCES `AM_EL_CommodityCatalog` (`Ix4_CommodityID`) ON DELETE NO ACTION,
    CONSTRAINT `fk_AM_EL_CommodityMaterials_AM_EL_MaterialFunction` FOREIGN KEY (`Ix4_MaterialFunctionID`) REFERENCES `AM_EL_MaterialFunction` (`Ix4_MaterialFunctionID`) ON DELETE NO ACTION,
    CONSTRAINT `fk_AM_EL_CommodityMaterials_AM_EL_MaterialData` FOREIGN KEY (`Ix4_MaterialID`) REFERENCES `AM_EL_MaterialData` (`Ix4_MaterialID`) ON DELETE NO ACTION
);

As reference, the generated Sql for SqlServer and SqLite

CREATE TABLE [AM_EL_CommodityMaterials] (
        [Ix4_CommodityID] int NOT NULL,
        [Ix4_MaterialFunctionID] int NOT NULL,
        [Ix4_MaterialID] int NULL,
        [Ix2_Material_VC] smallint NULL,
        CONSTRAINT [AM_EL_CommodityMaterials$pk_ETCommodityMaterials] PRIMARY KEY ([Ix4_CommodityID], [Ix4_MaterialFunctionID]),
        CONSTRAINT [AM_EL_CommodityMaterials$fk_AM_EL_CommodityMaterials_AM_EL_CommodityCatalog] FOREIGN KEY ([Ix4_CommodityID]) REFERENCES [AM_EL_CommodityCatalog] ([Ix4_CommodityID]) ON DELETE CASCADE,
        CONSTRAINT [AM_EL_CommodityMaterials$fk_AM_EL_CommodityMaterials_AM_EL_MaterialFunction] FOREIGN KEY ([Ix4_MaterialFunctionID]) REFERENCES [AM_EL_MaterialFunction] ([Ix4_MaterialFunctionID]) ON DELETE CASCADE,
        CONSTRAINT [AM_EL_CommodityMaterials$fk_AM_EL_CommodityMaterials_AM_EL_MaterialData] FOREIGN KEY ([Ix4_MaterialID]) REFERENCES [AM_EL_MaterialData] ([Ix4_MaterialID]) ON DELETE CASCADE
    );

CREATE TABLE "AM_EL_CommodityMaterials" (
    "Ix4_CommodityID" INTEGER NOT NULL,
    "Ix4_MaterialFunctionID" INTEGER NOT NULL,
    "Ix4_MaterialID" INTEGER NULL,
    "Ix2_Material_VC" INTEGER NULL,
    CONSTRAINT "AM_EL_CommodityMaterials$pk_ETCommodityMaterials" PRIMARY KEY ("Ix4_CommodityID", "Ix4_MaterialFunctionID"),
    CONSTRAINT "AM_EL_CommodityMaterials$fk_AM_EL_CommodityMaterials_AM_EL_CommodityCatalog" FOREIGN KEY ("Ix4_CommodityID") REFERENCES "AM_EL_CommodityCatalog" ("Ix4_CommodityID") ON DELETE CASCADE,
    CONSTRAINT "AM_EL_CommodityMaterials$fk_AM_EL_CommodityMaterials_AM_EL_MaterialFunction" FOREIGN KEY ("Ix4_MaterialFunctionID") REFERENCES "AM_EL_MaterialFunction" ("Ix4_MaterialFunctionID") ON DELETE CASCADE,
    CONSTRAINT "AM_EL_CommodityMaterials$fk_AM_EL_CommodityMaterials_AM_EL_MaterialData" FOREIGN KEY ("Ix4_MaterialID") REFERENCES "AM_EL_MaterialData" ("Ix4_MaterialID") ON DELETE CASCADE
);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MichaelSteineckecommented, Dec 15, 2021

As far as I can test it so far, the bug seems to be fixed.

1reaction
MichaelSteineckecommented, Dec 10, 2021

I’ll build a repo

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to do a count of fields in SQL with wrong datatype
Find all varchar fields in database and run try_cast over all rows in all tables. If ALL conversions pass for a column in...
Read more >
Pandas: Detect Mixed Data Types and Fix it
Infer Data Types First: df.info() doesn't always show the correct data type for a column with mixed data types; it might tell you...
Read more >
10 tricks for converting Data to a Numeric Type in Pandas
Converting multiple data columns at once; Defining data types when reading a CSV file; Creating a custom function to convert data type; astype() ......
Read more >
Select Columns with Specific Data Types in Pandas ...
In this article, we will see how to select columns with specific data types ... Finally, display the column having a particular data...
Read more >
Cassandra counter columns: Nice in theory, hazardous ...
The most obvious constraint is that counter columns can only exist in tables that are made up entirely of counter columns. That means...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found