0

I trying to create inheritance so I can have some super classes with general properties and then more specialized classes that are the ones getting used.

I want something like the Table per type or table per class they make here http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

So I have my supertype Game here

    public abstract class Game
{
    public virtual int Id { get; set; }
    public virtual List<UserGame> UserList { get; set; }
}

public class GameMap : ClassMap<Game>
{
    public GameMap()
    {
        Id(x => x.Id, "GameId")
            .GeneratedBy
            .HiLo("100");
        HasMany(x => x.UserList)
           .Cascade.All();
    }
}

And then my specialized class here QMGameActive

    public class QMGameActive : Game
{
    public virtual DateTime LastUpdate { get; set; }
    public virtual string SelectedBrick { get; set; }
    public virtual int Score { get; set; }
    public virtual string History { get; set; }
    public virtual int StartingPlayerId { get; set; }

    public QMGameActive() 
    {
        LastUpdate = DateTime.Now;
        History = "";
        SelectedBrick = "";
    }
}

public class QMGameActiveMap : SubclassMap<QMGameActive>
{
    public QMGameActiveMap()
    {
        KeyColumn("GameId");
        Map(x => x.LastUpdate);
        Map(x => x.SelectedBrick);
        Map(x => x.Score);
        Map(x => x.History);
        Map(x => x.StartingPlayerId);
    }
}

But when I get a diagram from the server I can see there is no connection between Game and QMGameActive there

enter image description here

So what am I missing to make it use inheritance?

3
  • what is the point of having this inheritance and creating a Game table with nothing but the Id? Commented Jun 25, 2012 at 19:14
  • Right now there is only one game in my database, but later on there will be multiple Games, each of them will inherit from Game which is the "supertype" and I will also make more subtypes as there will also be a QMGameInActive and I want my database diagram to look like codeproject.com/KB/library/mapping-strategies-fluent/4.png if possible Commented Jun 25, 2012 at 19:18
  • 1
    you might want to read up on this: ayende.com/blog/3941/nhibernate-mapping-inheritance Commented Jun 25, 2012 at 19:23

1 Answer 1

1

I am fairly sure that if you KeyColumn("GameId"); from the QMGameActiveMap() then NHibernate will generate QMGameActive with an ID Column of GameID which will be a foreign key of Game.GameId. which would seem to give you what you want.

(sorry away from home and cannot try code out to make sure).

Sign up to request clarification or add additional context in comments.

1 Comment

But I already have the KeyColumn("GameId"); in the QMGameActiveMap ? And that makes no difference if its their or not, just creates that "Index" you see in the picture

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.