2

我有以下模型,我想在数据库ShiftRequestMissionRequest有一个表。

    public class RequestBase
    {
        public int Id { get; set; }
        public DateTime? RequestDate { get; set; }
        public int UserId { get; set; }

        public virtual ICollection<Notification> Notifications { get; set; }

    }

    public class ShiftRequest : RequestBase
    {
        public virtual Column Column { get; set; }

    }

    public class MissionRequest : RequestBase
    {
        public virtual Mission Mission { get; set; }
    }

我尝试在该override void OnModelCreating(ModelBuilder modelBuilder)方法中执行此操作,但只RequestBases创建了一个表:

modelBuilder.Entity<ShiftRequest>().MapSingleType().ToTable("dbo.ShiftRequests");
modelBuilder.Entity<MissionRequest>().MapSingleType().ToTable("dbo.MissionRequest");

我究竟做错了什么?

编辑

Column并且Mission也是我模型中的实体,这可以接受吗?

4

1 回答 1

1

查看本文中关于 TPH 的部分。如果 Mission 和 Column 是复杂类型,您还将在那里找到如何映射它们。通常,您必须使用 MapHiearchy 和 Case 方法而不是 MapSingleType。

编辑:

这是示例:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;


namespace EFTest
{
    public class RequestBase
    {
        public int Id { get; set; }
        public DateTime? RequestedDate { get; set; }
        public int UserId { get; set; }
    }

    public class Mission
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<MissionRequest> MissionRequests { get; set; }
    }

    public class Column
    {
        public string Name { get; set; }
    }

    public class MissionRequest : RequestBase
    {
        public virtual Mission Mission { get; set; }
    }

    public class ShiftRequest : RequestBase
    {
        public Column Column { get; set; }
    }

    public class TestContext : DbContext
    {
        public DbSet<RequestBase> Requests { get; set; }
        public DbSet<Mission> Missions { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ContainerName = "EFTest";
            modelBuilder.IncludeMetadataInDatabase = false;

            // Example of complex type mapping. First you have to define 
            // complex type. Than you can access type properties in  
            // MapHiearchy.
            var columnType = modelBuilder.ComplexType<Column>();
            columnType.Property(c => c.Name).HasMaxLength(50);

            modelBuilder.Entity<Mission>()
                .Property(m => m.Id)
                .IsIdentity();

            modelBuilder.Entity<Mission>()
                .HasKey(m => m.Id)
                .MapSingleType(m => new { m.Id, m.Name })
                .ToTable("dbo.Missions");

            modelBuilder.Entity<RequestBase>()
                .Property(r => r.Id)
                .IsIdentity();

            // You map multiple entities to single table. You have to  
            // add some discriminator to differ entity type in the table. 
            modelBuilder.Entity<RequestBase>()
                .HasKey(r => r.Id)
                .MapHierarchy()
                .Case<RequestBase>(r => new { r.Id, r.RequestedDate, r.UserId, Discriminator = 0 })
                .Case<MissionRequest>(m => new { MissionId = m.Mission.Id, Discriminator = 1 })
                .Case<ShiftRequest>(s => new { ColumnName = s.Column.Name, Discriminator = 2 })
                .ToTable("dbo.Requests");
        }
    }
}

编辑2:

我更新了示例。现在 Mission 是实体而不是复杂类型。

于 2010-08-27T15:23:32.100 回答