为什么EF Code First为我的导航属性生成一列?

时间:2017-06-30 17:32:30

标签: c# entity-framework

我试图弄清楚如何配置EF Code First,以生成表格。我遇到的问题是我不知道如何正确配置表。该表与自身有父子关系。我更喜欢使用FluentAPI配置而不是属性的解决方案。

EF正在生成下表:

Specification
-------------
SpecificationId
Name
ParentSpecificationId
ParentSpecification_SpecificationId

这是班级:

public class Specification
{
    public Specification()
    {
        Children = new Collection<Specification>();
    }

    public int SpecificationId { get; set; }
    public string Name { get; set; }
    public int? ParentSpecificationId { get; set;}
    public virtual Specification ParentSpecification { get; set;}
    public virtual ICollection<Specification> Children { get; set;}
}

我根据this问题

尝试了以下配置
public class SpecificationConfiguration : EntityTypeConfiguration<Specification>
{
    public SpecificationConfiguration()
    {
        ToTable("Specification");
        HasKey(k => k.SpecificationId);
        Property(p => p.SpecificationId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        HasRequired(t => t.ParentSpecification)
        .WithMany(t => t.Children)
        .HasForeignKey(t => t.ParentSpecificationId)
        .WillCascadeOnDelete(false);

        HasOptional(t => t.Children)
        .WithMany()
        .HasForeignKey(t => t.SpecificationId)
        .WillCascadeOnDelete(false);
    }
}

我的配置有什么问题和/或如何让EF停止生成ParentSpecification_SpecificationId并将值放在ParentSpecificationId中?

注意:EF在正在生成的列中放入正确的值。

1 个答案:

答案 0 :(得分:1)

像这样(EF 6.1.3):

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{



    public class Specification
    {
        public int SpecificationId { get; set; }

        public string Name { get; set; }

        public int? ParentSpecificationId { get; set; }

        public virtual Specification ParentSpecification { get; set; }

        public virtual ICollection<Specification> Children { get; } = new HashSet<Specification>();
    }

    public class Db : DbContext
    {
        public DbSet<Specification> Specifications { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Specification>()
                        .HasMany(s => s.Children)
                        .WithOptional(s => s.ParentSpecification)
                        .HasForeignKey(s => s.ParentSpecificationId)
                        .WillCascadeOnDelete(false);

            base.OnModelCreating(modelBuilder);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {


            Database.SetInitializer(new DropCreateDatabaseAlways<Db>());

            using (var db = new Db())
            {
                db.Database.Log = m => Console.WriteLine(m);

                db.Database.Initialize(false);


                Console.ReadKey();
            }
        }
    }
}

输出

    CREATE TABLE [dbo].[Specifications] (
        [SpecificationId] [int] NOT NULL IDENTITY,
        [Name] [nvarchar](max),
        [ParentSpecificationId] [int],
        CONSTRAINT [PK_dbo.Specifications] PRIMARY KEY ([SpecificationId])
    )


    -- Executing at 6/30/2017 12:54:33 PM -05:00

    -- Completed in 24 ms with result: -1



    CREATE INDEX [IX_ParentSpecificationId] ON [dbo].[Specifications]([ParentSpecificationId])


    -- Executing at 6/30/2017 12:54:33 PM -05:00

    -- Completed in 8 ms with result: -1



    ALTER TABLE [dbo].[Specifications] 
    ADD CONSTRAINT [FK_dbo.Specifications_dbo.Specifications_ParentSpecificationId] 
    FOREIGN KEY ([ParentSpecificationId]) 
    REFERENCES [dbo].[Specifications] ([SpecificationId])