'EF Core 5 - Automatic Many to Many throws "property ... cannot be assigned a temporary value"
We have a very large context was generated using scaffolding a couple years ago, back in .Net Core 3. We are already running .NET 6 and I wanted to use the automatic Many to Many relationship creation that was released with EF Core 5 for a new feature, user alerts. The thing is I configured and created a migration and everything seems to be in place but when trying to create an alert for multiple users and link them with the many-to-many relationship an exception is thrown:
The property 'AlertUser (Dictionary<string, object>).AlertsId' cannot be assigned a temporary value.
Temporary values can only be assigned to properties configured to use store-generated values.
I can't find anything about it googling around so I know it can be something very specific to our model but everything seems fine:
Alert.cs
[Table("Alert")]
public class Alert
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("AlertID")]
public long Id { get; set; }
public string MessageText { get; set; }
public ICollection<User> Users { get; set; }
}
User.cs
[Table("Users")]
public class User
{
[Column("UserID")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public override long Id { get; set; }
[Column("Username")]
[Required]
[StringLength(255)]
[NonUnicode]
public override string UserName { get; set; }
public ICollection<Alert> Alerts { get; set; } = new HashSet<Alert>();
}
Basically a user can have many alerts and an alert can point to many users. To create an alert, I followed the official MSFT docs and several tutorials, I thought I could do something like:
var users = await _dbContext.Users
.Where(u => userIds.Contains(u.Id))
.ToListAsync();
var alert = new Alert
{
MessageText = message,
Users = users
};
await _dbContext.AddAsync(alert);
await _dbContext.SaveChangesAsync();
But the debugger never reaches SaveChanges and throws the exception in the await AddAsync. I've been trying a couple things that did work like creating the Alert and then updating the users with the alerts for each one, so I think it might be something with the way SQLServer is generating the keys or something like that. I wanted to know if someone else did face this error and what can we be doing wrong.
Also, here's the generated migration:
migrationBuilder.CreateTable(
name: "AlertUser",
columns: table => new
{
AlertsId = table.Column<long>(type: "bigint", nullable: false),
UsersId = table.Column<long>(type: "bigint", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AlertUser", x => new { x.AlertsId, x.UsersId });
table.ForeignKey(
name: "FK_AlertUser_Alert_AlertsId",
column: x => x.AlertsId,
principalTable: "Alert",
principalColumn: "AlertID",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_AlertUser_Users_UsersId",
column: x => x.UsersId,
principalTable: "Users",
principalColumn: "UserID",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_AlertUser_UsersId",
table: "AlertUser",
column: "UsersId");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
