Question

I am having the hardest time figuring the right configuration for mapping a foreign key "Category" to my "Products" table. When the model is being generated (Code First), I am getting this internal exception message.

{"Unable to determine the principal end of the 'DataLayer.Product_Category' relationship. Multiple added entities may have the same primary key."}

My POCO Product class:

        public partial class Product
    {
        public Product()
        {
            this.Carts = new List<Cart>();
            this.OrderLines = new List<OrderLine>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Measure { get; set; }
        public decimal Price { get; set; }
        public int ScheduleId { get; set; }
        public int CategoryId { get; set; }
        public virtual ICollection<Cart> Carts { get; set; }
        public virtual Category Category { get; set; }
        public virtual ICollection<OrderLine> OrderLines { get; set; }
        public virtual Schedule Schedule { get; set; }

        public ObjectState ObjectState { get; set; }
    }

My Category Class POCO:

        public partial class Category
    {
        public Category()
        {
            this.Products = new List<Product>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Product> Products { get; set; }

        public ObjectState ObjectState { get; set; }
    }

My Fluent API for Product:

  public class ProductMap : EntityTypeConfiguration<Product>
    {
        public ProductMap()
        {
            // Primary Key
            this.HasKey(x => x.Id);

            // Properties
            this.Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(x => x.Name)
                .IsRequired()
                .HasMaxLength(100);

            this.Property(x => x.Description)
                .IsRequired()
                .HasMaxLength(250);

            this.Property(x => x.Measure)
                .IsRequired()
                .HasMaxLength(30);

            // Table & Column Mappings
            this.ToTable("Products");
            this.Property(x => x.Id).HasColumnName("Id");
            this.Property(x => x.Name).HasColumnName("Name");
            this.Property(x => x.Description).HasColumnName("Description");
            this.Property(x => x.Measure).HasColumnName("Measure");
            this.Property(x => x.Price).HasColumnName("Price");
            this.Property(x => x.ScheduleId).HasColumnName("ScheduleId");
            this.Property(x => x.CategoryId).HasColumnName("CategoryId");

            // Relationships
            this.HasRequired(x => x.Category)
                .WithMany(x => x.Products)
                .HasForeignKey(d => d.CategoryId);
            this.HasRequired(x => x.Schedule)
                .WithMany(x => x.Products)
                .HasForeignKey(d => d.ScheduleId);

            this.Ignore(x => x.ObjectState);
        }
    }

My Fluent API to Category:

public class CategoryMap : EntityTypeConfiguration<Category>
{
    public CategoryMap()
    {
        // Primary Key
        this.HasKey(x => x.Id);

        // Properties
        this.Property(x => x.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(x => x.Name)
            .IsRequired()
            .HasMaxLength(20);

        // Table & Column Mappings
        this.ToTable("Categories");
        this.Property(x => x.Id).HasColumnName("Id");
        this.Property(x => x.Name).HasColumnName("Name");

        this.Ignore(x => x.ObjectState);
    }
}

The relationships are: Schedule is One To Many to Product Category is One to Many to Product

Any help would be appreciated.

Was it helpful?

Solution

Your problem is here:

this.Property(x => x.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

You should set it to DatabaseGeneratedOption.Identity.

In your case it adds records to table with same id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top