EF4代码只有马平继承 [英] EF4 Code only maping inheritance

查看:132
本文介绍了EF4代码只有马平继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了下面的模型,我想 ShiftRequest MissionRequest 有一个表在DB

I've got the following model and I want ShiftRequest and MissionRequest to have a single table in the DB.

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



我试图做到这一点在覆盖无效OnModelCreating(模型构建器模型构建器)的方法,但只有一个 RequestBases 创建表:

I've tried to do it in the override void OnModelCreating(ModelBuilder modelBuilder) method but only one RequestBases table is created:

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



我在做什么错了?

What am I doing wrong?

使命也是实体我模型,是可以接受的。

Column and Mission are also entities in my model, is that acceptable?

推荐答案

查看TPH节在这的文章。如果任务和列,复杂的类型,你还会发现有如何映射。 。通常你必须使用MapHiearchy和案例的方法,而不是MapSingleType

Check the section about TPH in this article. If Mission and Column are complex types you will also find there how to map them. Generally you have to use MapHiearchy and Case methods instead of 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:

我更新的例子。现在的使命是实体,而不是复杂类型。

I updated example. Now Mission is entity instead of complex type.

这篇关于EF4代码只有马平继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆