实体框架继承 - 解析具有相同父实体的实体之间的关系

时间:2015-12-18 16:04:57

标签: c# entity-framework ef-fluent-api

这是我的模特

enter image description here

我需要将此模型映射到Entity Framework,但我的结果只构建了一个名为Fluxo的表,并且有许多丑陋的FK。

此外,我不知道如何映射我在Fluxo课程中定义的PK

这是我的映射

public class FluxoPrincipalMap : EntityTypeConfiguration<FluxoPrincipal>
    {
        public FluxoPrincipalMap()
        {

            HasRequired(f => f.CasoDeUso).WithOptional(c => c.FluxoPrincipal);    

        }
    }


public class FluxoAlternativoMap : EntityTypeConfiguration<FluxoAlternativo>
    {
        public FluxoAlternativoMap()
        {
        }
    }

class FluxoDeExcecaoMap : EntityTypeConfiguration<FluxoDeExcecao>
    {
        public FluxoDeExcecaoMap()
        {

        }
    }

更新1 - 经过一些映射改进

好的,这是我的新映射

 public class FluxoPrincipalMap : EntityTypeConfiguration<FluxoPrincipal>
    {
        public FluxoPrincipalMap()
        {

            ToTable(nameof(FluxoPrincipal));

            HasKey(x => x.FluxoId);
            Property(x => x.FluxoId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            HasRequired(f => f.CasoDeUso).WithOptional(c => c.FluxoPrincipal);

            Ignore(f => f.ValidationResult);
            Ignore(f => f.IsValid);
        }
    }

public class FluxoAlternativoMap : EntityTypeConfiguration<FluxoAlternativo>
    {
        public FluxoAlternativoMap()
        {
            ToTable(nameof(FluxoAlternativo));

            HasKey(x => x.FluxoId);
            Property(x => x.FluxoId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            HasRequired(x => x.FluxoPrincipal)
                .WithMany(x => x.FluxosAlternativos)
                .HasForeignKey(x => x.FluxoPrincipalId);

            Ignore(x => x.ValidationResult);
            Ignore(x => x.IsValid);
        }
    }

class FluxoDeExcecaoMap : EntityTypeConfiguration<FluxoDeExcecao>
    {
        public FluxoDeExcecaoMap()
        {
            ToTable(nameof(FluxoDeExcecao));

            HasKey(x => x.FluxoId);
            Property(x => x.FluxoId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            HasRequired(x => x.FluxoPrincipal)
                .WithMany(x => x.FluxosDeExcecao)
                .HasForeignKey(x => x.FluxoPrincipalId);

            Ignore(x => x.ValidationResult);
            Ignore(x => x.IsValid);
        }
    }

这是我的上下文配置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Properties()
                // ReSharper disable once PossibleNullReferenceException
                .Where(p => p.Name == p.ReflectedType.Name + "Id")
                .Configure(p => p.IsKey());

            modelBuilder.Properties<string>()
                .Configure(p => p.HasColumnType("varchar"));

            modelBuilder.Properties<string>()
                .Configure(p => p.HasMaxLength(100));

            modelBuilder.Configurations.Add(new ProjetoMap());
            modelBuilder.Configurations.Add(new RequisitoMap());
            modelBuilder.Configurations.Add(new CasoDeUsoMap());
            modelBuilder.Configurations.Add(new FluxoPrincipalMap());
            modelBuilder.Configurations.Add(new FluxoAlternativoMap());
            modelBuilder.Configurations.Add(new FluxoDeExcecaoMap());

            base.OnModelCreating(modelBuilder);
        }

1 个答案:

答案 0 :(得分:1)

在Entity Framework中有三种使用继承的方法,你可以在这里看到所有这些:http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

THP

public class Context : DbContext
{
    public DbSet<Fluxo> Fluxo { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
    }
}

public class FluxoPrincipalMap : EntityTypeConfiguration<FluxoPrincipal>
{
    public FluxoPrincipalMap()
    {
        HasKey(x => x.FluxoID);
    }
}


public class FluxoAlternativoMap : EntityTypeConfiguration<FluxoAlternativo>
{
    public FluxoAlternativoMap()
    {
        HasKey(x => x.FluxoID);
    }
}

class FluxoDeExcecaoMap : EntityTypeConfiguration<FluxoDeExcecao>
{
    public FluxoDeExcecaoMap()
    {
        HasKey(x => x.FluxoID);
    }
}

TPT

public class Context : DbContext
{
    public DbSet<FluxoPrincipal> FluxoPrincipal { get; set; }
    public DbSet<FluxoAlternativo> FluxoAlternativo { get; set; }
    public DbSet<FluxoDeExcecao> FluxoDeExcecao { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
    }
}

public class FluxoPrincipalMap : EntityTypeConfiguration<FluxoPrincipal>
{
    public FluxoPrincipalMap()
    {
        HasKey(x => x.FluxoID);
    }
}


public class FluxoAlternativoMap : EntityTypeConfiguration<FluxoAlternativo>
{
    public FluxoAlternativoMap()
    {
        HasKey(x => x.FluxoID);
    }
}

class FluxoDeExcecaoMap : EntityTypeConfiguration<FluxoDeExcecao>
{
    public FluxoDeExcecaoMap()
    {
        HasKey(x => x.FluxoID);
    }
}

TPC

public class Context : DbContext
{
    public DbSet<FluxoPrincipal> FluxoPrincipal { get; set; }
    public DbSet<FluxoAlternativo> FluxoAlternativo { get; set; }
    public DbSet<FluxoDeExcecao> FluxoDeExcecao { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
    }
}

public class FluxoPrincipalMap : EntityTypeConfiguration<FluxoPrincipal>
{
    public FluxoPrincipalMap()
    {
        HasKey(x => x.FluxoID);
        Map(x => x.MapInheritedProperties());
    }
}


public class FluxoAlternativoMap : EntityTypeConfiguration<FluxoAlternativo>
{
    public FluxoAlternativoMap()
    {
        HasKey(x => x.FluxoID);
        Map(x => x.MapInheritedProperties());
    }
}

class FluxoDeExcecaoMap : EntityTypeConfiguration<FluxoDeExcecao>
{
    public FluxoDeExcecaoMap()
    {
        HasKey(x => x.FluxoID);
        Map(x => x.MapInheritedProperties());
    }
}