当前位置:  开发笔记 > 编程语言 > 正文

Cascade.SaveOrUpdate(SQLite)fluent-nhibernate

如何解决《Cascade.SaveOrUpdate(SQLite)fluent-nhibernate》经验,为你挑选了1个好方法。

我在两个班级之间有多对多的关系:比赛和球员

我在映射中设置了Cascade.SaveUpdate,但是在保存锦标赛实例时,玩家将不会保存到玩家表中.Nhibernate只在链接表中写入父键和子键列.DB是SQLite.

这些是映射

 public TournamentMap()
    {
        WithTable("Tournaments");
        Id(x => x.Name);
        Map(x => x.Start).Access.AsLowerCaseField();
        Map(x => x.End).Access.AsLowerCaseField();

        HasManyToMany(x => x.Players)
            .WithTableName("TournamentsPlayers")
            .WithParentKeyColumn("tournamentName")
            .WithChildKeyColumn("playerName").AsSet()
            .Access.AsLowerCaseField()
            .Cascade.SaveUpdate().LazyLoad();

        References(x => x.Type).Access.AsLowerCaseField()
            .TheColumnNameIs("typeName")
            .Cascade.SaveUpdate();
    }

  public class PlayerMap : ClassMap
{
    public PlayerMap()
    {
        WithTable("Players");
        Id(x => x.Name);

        HasManyToMany(x => x.Tournaments)
            .Access.AsLowerCaseField()
            .WithTableName("TournamentsPlayers")
            .WithParentKeyColumn("playerName")
            .WithChildKeyColumn("tournamentName").AsSet()
            .Cascade.SaveUpdate().IsInverse().LazyLoad();
    }
}

小智.. 5

也许有点(或很多)迟到了,但是,

我简化你的例子看起来像这样:

public class Tournament
{
    public virtual Guid Id { get; private set; }

    private readonly ISet players;

    public Tournament()
    {
        players = new HashedSet();
    }

    public virtual ISet Players
    {
        get { return players; }
    }

    public virtual void AddPlayer(Player player)
    {
        players.Add(player);
    }
}

public class Player 
{
    public virtual Guid Id { get; private set; }

    private readonly ISet tournaments;

    public Player()
    {
        tournaments = new HashedSet();
    }

    public virtual ISet Tournaments
    {
        get { return tournaments; }
    }

    public virtual void AddTournament(Tournament tournament)
    {
        tournaments.Add(tournament);
    }
}

public class TournamentMap : ClassMap
{
    public TournamentMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();

        HasManyToMany(x => x.Players)
            .AsSet().Access.AsLowerCaseField()
            .Cascade.SaveUpdate();
    }
}
public class PlayerMap : ClassMap
{
    public PlayerMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();

        HasManyToMany(x => x.Tournaments)
            .Access.AsLowerCaseField()
            .AsSet()
            .Cascade.SaveUpdate().Inverse();
    }
}

第一个测试看起来像:

   [Test]
    public void CanSavePlayerAttachedToTournament()
    {
        Player player = new Player();
        Tournament tournament = new Tournament();
        player.AddTournament(tournament);
        tournament.AddPlayer(player);
        Session.Save(tournament);
        Session.Flush();
    }

并产生以下sql:

NHibernate:INSERT INTO"锦标赛"(Id)VALUES(@ p0); 'p0 = '65559bba-8603-4874-8a8f-9bf4018596df'NHibernate:INSERT INTO"Player"(Id)VALUES(@ p0); @ p0 ='549e10​​7c-1339-4fab-8960-9bf4018596e8'NHibernate:INSERT INTO PlayerToTournament(Tournament_id,Player_id)VALUES(@ p0,@ p1); @ p0 = '65559bba-8603-4874-8a8f-9bf4018596df',@ p1 ='549e10​​7c-1339-4fab-8960-9bf4018596e8'

第二个测试看起来像:

    [Test]
    public void CanSaveTounamentAttachedToPlayer()
    {
        Player player = new Player();
        Tournament tournament = new Tournament();
        player.AddTournament(tournament);
        tournament.AddPlayer(player);
        Session.Save(player);
        Session.Flush();
    }

并产生以下sql:

NHibernate:INSERT INTO"Player"(Id)VALUES(@ p0); @ p0 = '35c078c5-1102-4c63-91ca-9bf40185971c'NHibernate:INSERT INTO"锦标赛"(Id)VALUES(@ p0); 'p0 ='367898cf-5835-4e1b-9d7d-9bf40185971c'NHibernate:INSERT INTO PlayerToTournament(Tournament_id,Player_id)VALUES(@ p0,@ p1); @ p0 = '367898cf-5835-4e1b-9d7d-9bf40185971c',@ p1 = '35c078c5-1102-4c63-91ca-9bf40185971c'



1> 小智..:

也许有点(或很多)迟到了,但是,

我简化你的例子看起来像这样:

public class Tournament
{
    public virtual Guid Id { get; private set; }

    private readonly ISet players;

    public Tournament()
    {
        players = new HashedSet();
    }

    public virtual ISet Players
    {
        get { return players; }
    }

    public virtual void AddPlayer(Player player)
    {
        players.Add(player);
    }
}

public class Player 
{
    public virtual Guid Id { get; private set; }

    private readonly ISet tournaments;

    public Player()
    {
        tournaments = new HashedSet();
    }

    public virtual ISet Tournaments
    {
        get { return tournaments; }
    }

    public virtual void AddTournament(Tournament tournament)
    {
        tournaments.Add(tournament);
    }
}

public class TournamentMap : ClassMap
{
    public TournamentMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();

        HasManyToMany(x => x.Players)
            .AsSet().Access.AsLowerCaseField()
            .Cascade.SaveUpdate();
    }
}
public class PlayerMap : ClassMap
{
    public PlayerMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();

        HasManyToMany(x => x.Tournaments)
            .Access.AsLowerCaseField()
            .AsSet()
            .Cascade.SaveUpdate().Inverse();
    }
}

第一个测试看起来像:

   [Test]
    public void CanSavePlayerAttachedToTournament()
    {
        Player player = new Player();
        Tournament tournament = new Tournament();
        player.AddTournament(tournament);
        tournament.AddPlayer(player);
        Session.Save(tournament);
        Session.Flush();
    }

并产生以下sql:

NHibernate:INSERT INTO"锦标赛"(Id)VALUES(@ p0); 'p0 = '65559bba-8603-4874-8a8f-9bf4018596df'NHibernate:INSERT INTO"Player"(Id)VALUES(@ p0); @ p0 ='549e10​​7c-1339-4fab-8960-9bf4018596e8'NHibernate:INSERT INTO PlayerToTournament(Tournament_id,Player_id)VALUES(@ p0,@ p1); @ p0 = '65559bba-8603-4874-8a8f-9bf4018596df',@ p1 ='549e10​​7c-1339-4fab-8960-9bf4018596e8'

第二个测试看起来像:

    [Test]
    public void CanSaveTounamentAttachedToPlayer()
    {
        Player player = new Player();
        Tournament tournament = new Tournament();
        player.AddTournament(tournament);
        tournament.AddPlayer(player);
        Session.Save(player);
        Session.Flush();
    }

并产生以下sql:

NHibernate:INSERT INTO"Player"(Id)VALUES(@ p0); @ p0 = '35c078c5-1102-4c63-91ca-9bf40185971c'NHibernate:INSERT INTO"锦标赛"(Id)VALUES(@ p0); 'p0 ='367898cf-5835-4e1b-9d7d-9bf40185971c'NHibernate:INSERT INTO PlayerToTournament(Tournament_id,Player_id)VALUES(@ p0,@ p1); @ p0 = '367898cf-5835-4e1b-9d7d-9bf40185971c',@ p1 = '35c078c5-1102-4c63-91ca-9bf40185971c'

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有