实体框架,代码优先外键映射错误

时间:2013-04-16 06:49:39

标签: entity-framework poco fluent-interface

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

{“无法确定'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);
        }
    }

我的Fluent 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);
    }
}

关系是: 附表是产品的一对多 类别是产品中的一对多

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

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

您应该将其设置为DatabaseGeneratedOption.Identity

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