0

我很难确定将外键“类别”映射到我的“产品”表的正确配置。生成模型时(代码优先),我收到此内部异常消息。

{“无法确定 'DataLayer.Product_Category' 关系的主体端。多个添加的实体可能具有相同的主键。”}

我的 POCO 产品类别:

        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; }
    }

我的类别类 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; }
    }

我的 Fluent API 产品:

  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);
        }
    }

我的流利 API 到类别:

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);
    }
}

这些关系是: 计划是一对多到产品类别是一对多到产品

任何帮助,将不胜感激。

4

1 回答 1

0

你的问题在这里:

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

您应该将其设置为DatabaseGeneratedOption.Identity.

在您的情况下,它将记录添加到具有相同 ID 的表中。

于 2013-04-16T06:54:49.417 回答