Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Composite Key, one foreign, one auto incrementing

I have this entity:

public class PlayerScoreHistory
{
    public int Id { get; set; }

    public int PlayerScoreId { get; set; }

    public DateTime ScoreWhen { get; set; }
}

I need to make a composite key out of both of those Id fields. PlayerScoreId needs to be a foreign key to PlayerScore.Id . Id needs to be an auto incrementing id.

So, I got as far as:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<PlayerScoreHistory>()
        .HasKey(x => new { x.Id, x.PlayerScoreId });
}

This gives me the composite key. I did Add-Migration Initial to give me my initial migration.

To get the Foreign Key, I simply added the line in the constraints parameter:

 migrationBuilder.CreateTable(
    name: "PlayerScoreHistories",
    columns: table => new
        {
            Id = table.Column<int>(nullable: false),
            PlayerScoreId = table.Column<int>(nullable: false),
            ScoreWhen = table.Column<DateTime>(nullable: false)
        },
    constraints: table =>
        {
            table.PrimaryKey("PK_PlayerScoreHistories", x => new { x.Id, x.PlayerScoreId });
            table.ForeignKey("FK_PlayerScoreId", arg => new {  arg.PlayerScoreId}, "PlayerScores", "Id");
        });

So two questions:

  1. How can I get the foreign key creation in the OnModelCreating method?
  2. How can I make the Id column a Database Generated field and make sure EF Core doesn't try and set a value?

I'm not exactly sure what options are open to me, seeing as EF Core is extremely new...

The two errors I get:

1.

When I add this line to the Id column parameter configuration:

Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)

I get

SQL Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF [duplicate]

2.

When I remove the line and then try and add an entity, I get:

Can't insert null into column Id


Clearly I'm in between somewhere....


Edit So Far

I decided to remove the Id column and just use PlayerScoreId and ScoreWhen as the composite keys....

But, one question I still have is how to make OnModelCreating identity PlayerScoreId as a foreign key - without having navigation properties.....

like image 765
Callum Linington Avatar asked Mar 11 '23 10:03

Callum Linington


1 Answers

But, one question I still have is how to make OnModelCreating identity PlayerScoreId as a foreign key - without having navigation properties.....

You can use the HasOne / WithMany (or HasMany / WithOne) methods w/o specifying the navigation property, combined with HasForeignKey as usual:

modelBuilder.Entity<PlayerScoreHistory>()
    .HasOne<PlayerScore>()
    .WithMany()
    .HasForeignKey(e => e.PlayerScoreId);
like image 88
Ivan Stoev Avatar answered Mar 24 '23 05:03

Ivan Stoev