I have an existing database where I have four identical and unrelated tables.
I want to use the same POCO class to describe all four without having to create duplicates of the same class.
This is what my context looks like so far:
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; }
}
My problem is that the existing tables are named "map_ratings_vsh" and "map_ratings_jump", and I cannot use the data annotations TableAttribute
because it can only be used on the class.
Is there some other way--maybe the fluent api--to describe my schema?
One way I've found to solve this is to use inheritance.
[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; }
}
Then you can have your DbContext look like:
public class StatsContext : DbContext
{
public DbSet<MapRatingsVSH> MapRatingsVSH { get; set; }
public DbSet<MapRatingsJump> MapRatingsJump { get; set; }
}
EF shouldn't have any problem understanding that these are two different tables even though the implementation will be in the same place (MapRatingsBase
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With