Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有相同POCO类的多个表_C#_Entity Framework_Entity Framework 4.1_Ef Code First - Fatal编程技术网

C# 具有相同POCO类的多个表

C# 具有相同POCO类的多个表,c#,entity-framework,entity-framework-4.1,ef-code-first,C#,Entity Framework,Entity Framework 4.1,Ef Code First,我有一个现有的数据库,其中有四个相同和不相关的表 我想使用相同的POCO类来描述这四个类,而不必创建相同类的副本 到目前为止,我的上下文是这样的: class StatsContext : DbContext { // [MagicTableAttribute( "map_ratings_vsh" )] -- does something like this exist? public DbSet<MapRatings> MapRatingsVSH { get; se

我有一个现有的数据库,其中有四个相同和不相关的表

我想使用相同的POCO类来描述这四个类,而不必创建相同类的副本

到目前为止,我的上下文是这样的:

class StatsContext : DbContext
{
    // [MagicTableAttribute( "map_ratings_vsh" )] -- does something like this exist?
    public DbSet<MapRatings> MapRatingsVSH { get; set; }

    public DbSet<MapRatings> MapRatingsJump { get; set; }

    // 2 more tables using same class
}

class MapRatings
{
    public string SteamID { get; set; }

    public string Map { get; set; }

    public int Rating { get; set; }

    [Column( "rated" )]
    public DateTime Time { get; set; }
}
类statContext:DbContext
{
//[MagicTableAttribute(“map_ratings_vsh”)]——是否存在类似的情况?
公共数据库集映射Svsh{get;set;}
公共数据库集映射cump{get;set;}
//使用同一类的另外两个表
}
类映射评级
{
公共字符串streamid{get;set;}
公共字符串映射{get;set;}
公共整数评级{get;set;}
[列(“额定”)]
公共日期时间{get;set;}
}
我的问题是现有的表被命名为“map\u ratings\u vsh”和“map\u ratings\u jump”,我不能使用数据注释
TableAttribute
,因为它只能在类上使用


是否有其他方法(可能是fluent api)来描述我的模式?

我过去处理这个问题的(哈克)方法是使用存储过程或视图返回数据,然后提供“AddEntity”或“SaveEntity”方法,用于持久化有问题的实体。您可以使用fluent api将某些属性映射到一个表,将其他属性映射到另一个表,如下所示:

modelBuilder.Entity<TestResult>()
    .Map(m =>
    {
        m.Properties(t => new { /* map_ratings_vsh columns */ });
        m.ToTable("map_ratings_vsh");
    })
    .Map(m =>
    {
        m.Properties(t => new { /* map_ratings_jump columns */ });
        m.ToTable("map_ratings_jump");
    });
modelBuilder.Entity()
.Map(m=>
{
m、 属性(t=>新的{/*map\u ratings\u vsh列*/});
m、 ToTable(“map_评级_vsh”);
})
.Map(m=>
{
m、 属性(t=>new{/*map\u ratings\u jump columns*/});
m、 ToTable(“地图等级跳跃”);
});

我发现解决这个问题的一种方法是使用继承

[Table("map_ratings_vsh")]
public class MapRatingsVSH : MapRatingsBase {}

[Table("map_ratings_jump")]
public class MapRatingsJump : MapRatingsBase {}

public class MapRatingsBase
{
    public string SteamID { get; set; }

    public string Map { get; set; }

    public int Rating { get; set; }

    [Column( "rated" )]
    public DateTime Time { get; set; }
}
然后,您可以使DbContext看起来像:

public class StatsContext : DbContext
{

    public DbSet<MapRatingsVSH> MapRatingsVSH { get; set; }

    public DbSet<MapRatingsJump> MapRatingsJump { get; set; }

}
公共类statContext:DbContext
{
公共数据库集映射Svsh{get;set;}
公共数据库集映射cump{get;set;}
}

EF理解这是两个不同的表应该不会有任何问题,即使实现将在同一个位置(
MapRatingsBase

通常,一个表只有一个表,然后使用鉴别器列来确定每种类型的实体。为什么你不想这样做?@MystereMan,我完全同意,但我不得不使用这个现有的模式。这就是我最终要做的,所以我接受这个答案。谢谢