地图TPT战略实体框架代码拳头[英] Map TPT Strategy Entity Framework Code fist

本文是小编为大家收集整理的关于地图TPT战略实体框架代码拳头的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

im首先处理EF代码,我喜欢抽象!所以想要这样的itemcat实体:

public abstract class EntityBase
    {
        public int Id { get; set; }
    }

public abstract  class TreeBase : EntityBase
    {
        public int? ParentId { get; set; }

        public string Name { get; set; }

        public virtual TreeBase Parent { get; set; }
        public virtual ICollection<TreeBase> Children { get; set; }

    }

public  abstract class CatBase : TreeBase
    {

        public string ImageUrl { get; set; }

        public string Description { get; set; }


        public int OrderId { get; set; }

    }

public class ItemCat : CatBase
    {
        public stringName { get; set; }

// other fields...
        public virtual ICollection<Item> Items { get; set; }

    }

我的地图stintgey是每个类型的表.tpt

所有用于ItemCat的基类都是通过抽象关键字装饰的.但是在迁移中,我在DB中获得了Treebase表,为什么?我想知道这是抽象的.我的映射需要明确定义任何配置吗?我正在使用EF 6

edit 在迁移中也可以为树库表创建歧视列,当我插入记录时,它具有itemcat值.

编辑

protected override void OnModelCreating(DbModelBuilder mb)
        {
            //Item

            mb.Configurations.Add(new TreeBaseConfig());
            mb.Configurations.Add(new CatConfig());

        }

 public class TreeBaseConfig:EntityTypeConfiguration<TreeBase>
    {
        public TreeBaseConfig()
        {
            HasMany(rs => rs.Children).WithOptional(rs => rs.Parent).HasForeignKey(rs => rs.ParentId).WillCascadeOnDelete(false);
        }
    }

public  class CatConfig : EntityTypeConfiguration<CatBase>
    {
        public CatConfig()
        {
            //properties
            Property(rs => rs.Name).IsUnicode();
            Property(rs => rs.ImageUrl).IsUnicode();
            Property(rs => rs.Description).IsUnicode();
        }
    }

编辑

我添加了itemcatConfig类:

public ItemCatConfig()
    {

        //map
       Map(m =>  { m.ToTable("ItemCats"); m.MapInheritedProperties(); });
    }

但是得到:

无法按定义的" itemcat"类型映射,因为它映射 使用实体分裂或其他类型的继承属性 继承的形式.要么选择其他继承映射 策略以不映射继承的属性,或更改所有类型 映射继承属性和不使用分裂的层次结构.

推荐答案

当您不做任何ToTable()映射时,您已经使用了TPH(每个层次结构表),而在执行ToTable()和MapInheritedProperties()>时,则使用了TPC(每个混凝土类型).如果要使用tpt(每种类型的表)仅执行ToTable()映射,然后将呼叫与MapInheritedProperties()关闭,例如:

:

ToTable("ItemCats");

本文地址:https://www.itbaoku.cn/post/685025.html

问题描述

Im working on EF Code first, I like abstraction! so want have ItemCat entity like this:

public abstract class EntityBase
    {
        public int Id { get; set; }
    }

public abstract  class TreeBase : EntityBase
    {
        public int? ParentId { get; set; }

        public string Name { get; set; }

        public virtual TreeBase Parent { get; set; }
        public virtual ICollection<TreeBase> Children { get; set; }

    }

public  abstract class CatBase : TreeBase
    {

        public string ImageUrl { get; set; }

        public string Description { get; set; }


        public int OrderId { get; set; }

    }

public class ItemCat : CatBase
    {
        public stringName { get; set; }

// other fields...
        public virtual ICollection<Item> Items { get; set; }

    }

My map startgey is table per type.TPT

All base classes for ItemCat are decoreated by abstract keyword. but in Migration i get TreeBases Table in Db, really why? i'm wonder because it's abstract. does my mapping need define any configuration explicitly? im using EF 6

Edit also EF in Migration create Discriminator column for TreeBase table and when i insert Record it has ItemCat value.

Edit

protected override void OnModelCreating(DbModelBuilder mb)
        {
            //Item

            mb.Configurations.Add(new TreeBaseConfig());
            mb.Configurations.Add(new CatConfig());

        }

 public class TreeBaseConfig:EntityTypeConfiguration<TreeBase>
    {
        public TreeBaseConfig()
        {
            HasMany(rs => rs.Children).WithOptional(rs => rs.Parent).HasForeignKey(rs => rs.ParentId).WillCascadeOnDelete(false);
        }
    }

public  class CatConfig : EntityTypeConfiguration<CatBase>
    {
        public CatConfig()
        {
            //properties
            Property(rs => rs.Name).IsUnicode();
            Property(rs => rs.ImageUrl).IsUnicode();
            Property(rs => rs.Description).IsUnicode();
        }
    }

Edit

I added ItemCatConfig Class:

public ItemCatConfig()
    {

        //map
       Map(m =>  { m.ToTable("ItemCats"); m.MapInheritedProperties(); });
    }

but get:

The type 'ItemCat' cannot be mapped as defined because it maps inherited properties from types that use entity splitting or another form of inheritance. Either choose a different inheritance mapping strategy so as to not map inherited properties, or change all types in the hierarchy to map inherited properties and to not use splitting.

推荐答案

You have used TPH (Table Per Hierarchy) when you don't do any ToTable() mapping, and TPC (Table Per Concrete Type) when doing both ToTable() and MapInheritedProperties(). If you want to use TPT (Table Per Type) do only the ToTable() mapping and leave the call to MapInheritedProperties() off, like so:

ToTable("ItemCats");