C
C#6mo ago
Rename

"Duplicate property" Entity Framework 8 Error

I've got this error popping up on OnModelCreating method:
System.InvalidOperationException: 'The property or navigation 'Rotation' cannot be added to the 'AlternativeShift' type because a property or navigation with the same name already exists on the 'AlternativeShift' type.'
System.InvalidOperationException: 'The property or navigation 'Rotation' cannot be added to the 'AlternativeShift' type because a property or navigation with the same name already exists on the 'AlternativeShift' type.'
this is how I'm setting up AlternativeShift there:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<AlternativeShift>(entity =>
{
entity.HasKey(e => e.Id)
.HasName("PK__Alternat__3214EC068342A05F")
.IsClustered(false);

entity.HasIndex(e => e.Rotation, "IX_AlternativeShifts->Rotation");

entity.HasIndex(e => e.Shift, "IX_AlternativeShifts->Shift");

entity.HasIndex(e => new { e.Rotation, e.Shift, e.Weekday }, "UQ_RotaShiftWeekday").IsUnique();

entity.Property(e => e.Id).ValueGeneratedNever();

entity.HasOne(d => d.Rotation).WithMany(p => p.AlternativeShifts)
.HasForeignKey(d => d.RotationId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_AlternativeShift.Rotation");

entity.HasOne(d => d.Shift).WithMany(p => p.AlternativeShifts)
.HasForeignKey(d => d.ShiftId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_AlternativeShift.Shift");

entity.HasOne(d => d.Weekday).WithMany(p => p.AlternativeShifts)
.HasForeignKey(d => d.WeekdayId)
.HasConstraintName("FK__Alternati__Weekd__1352D76D");
});
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<AlternativeShift>(entity =>
{
entity.HasKey(e => e.Id)
.HasName("PK__Alternat__3214EC068342A05F")
.IsClustered(false);

entity.HasIndex(e => e.Rotation, "IX_AlternativeShifts->Rotation");

entity.HasIndex(e => e.Shift, "IX_AlternativeShifts->Shift");

entity.HasIndex(e => new { e.Rotation, e.Shift, e.Weekday }, "UQ_RotaShiftWeekday").IsUnique();

entity.Property(e => e.Id).ValueGeneratedNever();

entity.HasOne(d => d.Rotation).WithMany(p => p.AlternativeShifts)
.HasForeignKey(d => d.RotationId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_AlternativeShift.Rotation");

entity.HasOne(d => d.Shift).WithMany(p => p.AlternativeShifts)
.HasForeignKey(d => d.ShiftId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_AlternativeShift.Shift");

entity.HasOne(d => d.Weekday).WithMany(p => p.AlternativeShifts)
.HasForeignKey(d => d.WeekdayId)
.HasConstraintName("FK__Alternati__Weekd__1352D76D");
});
8 Replies
Rename
RenameOP6mo ago
this is my AlternativeShift class: and this is my AlternativeShift class:
namespace Domain
{
using System;
using System.ComponentModel.DataAnnotations.Schema;

public class AlternativeShift
{
public Guid Id { get; set; }

[Column("Rotation")]
public Guid RotationId { get; set; }

[Column("Shift")]
public Guid ShiftId { get; set; }

[Column("Weekday")]
public Guid? WeekdayId { get; set; }

public Weekday Weekday { get; set; }

public Rotation Rotation { get; set; }

public Shift Shift { get; set; }
}
}
namespace Domain
{
using System;
using System.ComponentModel.DataAnnotations.Schema;

public class AlternativeShift
{
public Guid Id { get; set; }

[Column("Rotation")]
public Guid RotationId { get; set; }

[Column("Shift")]
public Guid ShiftId { get; set; }

[Column("Weekday")]
public Guid? WeekdayId { get; set; }

public Weekday Weekday { get; set; }

public Rotation Rotation { get; set; }

public Shift Shift { get; set; }
}
}
Jimmacle
Jimmacle6mo ago
[Column("Rotation")]
public Guid RotationId { get; set; }

public Rotation Rotation { get; set; }
[Column("Rotation")]
public Guid RotationId { get; set; }

public Rotation Rotation { get; set; }
is probably confusing it, you don't need that attribute
jcotton42
jcotton426mo ago
Also I personally would name that column RotationId anyways.
Rename
RenameOP6mo ago
well, it's named as Rotation in the DB, which is why I'm adding that. The deal is that it was a legacy website which I started to update to .net 8 But eitherway even removing the Column annotation the same error pops up!
dreadfullydistinct
if you have an existing database, is there a reason you aren't using scaffolding / reverse-engineering?
Rename
RenameOP6mo ago
I did, thats how that class AlternativeShift was created initially. I simply added the column data annotations. Used to work before!
dreadfullydistinct
Ahh I see
Rename
RenameOP6mo ago
looking further into the modelBuilder, I can see there's two RotationIds, one being a shadow property. not sure why this would get created as I already have RotationId field
No description
Want results from more Discord servers?
Add your server