EF4 仅代码映射继承

发布于 2024-09-15 23:42:19 字数 990 浏览 8 评论 0原文

我有以下模型,我希望 ShiftRequest 和 MissionRequest 在数据库中有一个表。

    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");

我做错了什么?

编辑

ColumnMission 也是我的模型中的实体,可以接受吗?

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

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?

EDIT

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-09-22 23:42:19

检查此 文章。如果任务和列是复杂类型,您还可以在那里找到如何映射它们。通常,您必须使用 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:

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

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.

Edit:

Here is the example:

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

Edit 2:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文