1

One person has One Title. (A title can be Mr. Mrs, Dr. etc). When I am trying to save this record (Code not Included), I am getting the following error.

InnerException = {"Cannot insert duplicate key row in object 'dbo.Persons' with unique index 'IX_Persons_TitleId'. The duplicate key value is (1).\r\nThe statement has been terminated."}

When debugging, I noticed that the Person object, has the Title object saved within it and the Title object again has the Person object in it. It goes on and on. I think this error is due to this. Anyway, I have attached the Model classes and Entity Configuration class. There might be something o do with the EntityConfiguration class. Please help

public class Title: Entity
{
    public int Id { get; set; }
    public string Name{ get; set; }

    public Person Person { get; set; }

}

public class Person: Entity
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int TitleId { get; set; }

    public Title Title { get; set; }
}


 public class PersonEntityConfiguration : IEntityTypeConfiguration<Person>
{
    public void Configure(EntityTypeBuilder<Doctor> builder)
    {
        builder.ToTable("Persons");

        builder.HasKey(c => c.Id);
        builder.HasOne(c => c.Title)
          .WithOne(c => c.Person)
          .HasForeignKey<Person>(cc => cc.TitleId)
          .OnDelete(DeleteBehavior.Restrict);


    }
}

CODE FOR SAVING

        _dbContext.Set<Person>().Add(entity);
        await _dbContext.SaveChangesAsync();

HOW ENTITY IS CREATED

var person = MyMapper.Mapper.Map<Person>(request);
Title title = await _titleRepository.GettitleById(person.TitleId);
person.Title = title;

 ...

_dbContext.Set<Person>().Add(entity);
await _dbContext.SaveChangesAsync();
8
  • Please add the code for saving Commented Jan 28, 2021 at 21:49
  • @GuruStron Added. Please have a look. Commented Jan 28, 2021 at 21:51
  • A title only has one person? Commented Jan 28, 2021 at 21:58
  • @caius I corrected that based on @ sergey's solution, but end up with the same error. Commented Jan 28, 2021 at 22:01
  • How entity is created? How do you get the title for it? Commented Jan 28, 2021 at 22:04

2 Answers 2

4

You have one to many relations, since several people can have the same title( Mr. for example). So the same title can have several people:

public class Title: Entity
{
    public int Id { get; set; }
    public string Name{ get; set; }

    public ICollection<Person> Persons { get; set; }

}

You maybe don' need db context relations at all. But you can try to change to this:

builder.HasOne(c => c.Title)
.WithMany(c => c.Person)

Or better replace all relations with this:


  modelBuilder.Entity<Person>(entity =>
            {
       entity.HasOne(d => d.Title)
    .WithMany(p => p.Persons)
    .HasForeignKey(d => d.TitleId)
     .OnDelete(DeleteBehavior.ClientSetNull);
         }

And check your Person db table. It can have Unique TitleId restriction that was created by your previous context relations. Just delete this restriction if it is there.

Sign up to request clarification or add additional context in comments.

9 Comments

Then how about the EntityConfiguration class ?
If you use EF core 5 you maybe don' t need it . You have to try.
I tried it and I still end up with the same error. Help ! :(
I tried out, but end up with the same error.
Did you change db context?
|
-1

Or try:

builder.HasIndex(x => x.TitleId).IsUnique(false);

1 Comment

It cannot be helpful solution, It's just a way to run away the main problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.