Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One to many relationship in Entity Framework 6

I have two simple tables that have to map to columns in an existing database:

public class StockItem
{
    public System.Guid pkStockItemID { get; set; }

    public virtual List<StockItem_ExtendedProperties> ExtendedProperties { get; set; }
}

public class StockItem_ExtendedProperties
{
    public System.Guid pkStockItem_ExtendedPropertiesID { get; set; }

    public System.Guid fkStockItemId { get; set; }

    public virtual StockItem StockItem { get; set; }
}

Following are the configuration classes for both:

public StockItemConfiguration ()
{
    this.HasMany(item => item.ExtendedProperties)
        .WithRequired(property => property.StockItem)
        .HasForeignKey(property => property.fkStockItemId)
        .WillCascadeOnDelete();
}

public StockItem_ExtendedPropertiesConfiguration ()
{
    this.HasRequired(property => property.StockItem)
        .WithMany(item => item.ExtendedProperties)
        .HasForeignKey(property => property.fkStockItemId)
        .WillCascadeOnDelete();
}

This results in the error: The item with identity 'StockItem_ExtendedProperties' already exists in the metadata collection. Parameter name: item.

It appears that defining the relationship from both sides of the table is causing the error. However, removing the configuration from either side still results in the same error.

In addition to how to fix the above, I'd like to know what the reason is and which of the two tables in such a relationship should be configured. Thanks.

like image 311
Raheel Khan Avatar asked Nov 22 '13 07:11

Raheel Khan


People also ask

What is one-to-many relationship in Entity Framework?

A one-to-many relationship happens when the primary key of one table becomes foreign keys in another table and also this primary key should participate in the primary key of the second table.

How do you set a one to one relationship in Entity Framework?

We can configure a one-to-One relationship between entities using Fluent API where both ends are required, meaning that the Student entity object must include the StudentAddress entity object and the StudentAddress entity must include the Student entity object in order to save it.

How do I create a many-to-many relationship in Entity Framework?

A many-to-many relationship is defined in code by the inclusion of collection properties in each of the entities - The Categories property in the Book class, and the Books property in the Category class: public class Book. { public int BookId { get; set; }

What is meant by many-to-many relationship in EF core?

Many-to-many relationships require a collection navigation property on both sides. They will be discovered by convention like other types of relationships. The way this relationship is implemented in the database is by a join table that contains foreign keys to both Post and Tag .


2 Answers

Turns out the problem was the underscore in the entity name StockItem_ExtendedProperties. Removing that got rid of the error. Using EF 6.0.0.0 which was the latest available version via NuGet.

like image 62
Raheel Khan Avatar answered Sep 19 '22 20:09

Raheel Khan


I have that exactly problem too. A class named 'Paquete_PartNumber' throws the same error. Removing the underscore solves the issue.

Using EF 6.0.1.

like image 38
cgonzalezr Avatar answered Sep 17 '22 20:09

cgonzalezr