使用NHibernate在联结表中创建复合主键

时间:2017-09-30 13:07:53

标签: c# nhibernate

请参阅以下代码:

    <style>
        .navbar-default {
            background-color: black !important;
            opacity: .6 !important;
            min-height:90px !important;
            border-radius: 5px !important;
            position: fixed; !important;
            width: 100%; !important;
            z-index: 100 !important;
        }
        .nav li {
            padding-top: 20px;
            padding-right: 15px;
        }
        .nav li a {
            color: white !important;
            font-size: 1.2em !important;
        }
        .navbar-brand {
            padding-right: 50px !important;
        } 
        .topInfo {
            background-image: url('/CMS_Static/Uploads/313864614C6F6F/miami beach-1.jpg');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
            height: 684px;
        }
        .topInfoText {
            color: white;
            padding-top: 220px;
        }
    </style>
        <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <!-- Bootstrap -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
      </head>
      <body>
        <nav class="navbar-default">
      <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>
    
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li><a href="#">Home</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Locations</a></li>
            <li><a href="#">Prices</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    
    <section class="topInfo">
        <div class="container">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <h1 class=" topInfoText text-center">Aerial Photography In South Florida</h1>
                    
                </div>
            </div>
            
        </div>
    </section>
    
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
      </body>
    </html>

它按照我的预期工作,即创建了三个表:Person;体育和人员运动。 PersonSport是支持人与运动之间多对多关系的联结表。

如何在PersonSport表(Personid,SportId)上创建复合主键?请查看已注释掉的两行,其中显示了我到目前为止所尝试的内容。

1 个答案:

答案 0 :(得分:0)

如果我添加以下类,它会按预期工作:

public class SportMap : ClassMapping<Sport>
    {
        public SportMap()
        {
            Id<Guid>(x => x.Id);
            Property<string>(x => x.Description);
            Bag(x => x.people, collectionMapping =>
            {
                collectionMapping.Table("PersonSports");
                //collectionMapping.Key(keyMapper => keyMapper.Column("SportId"));
                collectionMapping.Cascade(Cascade.None);
                collectionMapping.Key(k => k.Column("PersonId"));
            },
            map => map.ManyToMany(p => p.Column("SportId")));
        }
    }

    public class PersonSports
    {
        public virtual Guid SportId { get; set; }
        public virtual Guid PersonId { get; set; }

        public override bool Equals(object obj)
        {
            return true;
        }

        public override int GetHashCode()
        {
            return 1;
        }
    }

    public class PersonSportsMap : ClassMapping<PersonSports>
    {
        public PersonSportsMap()
        {
            ComposedId(map =>
            {
                map.Property(x => x.PersonId);
                map.Property(x => x.SportId);
            });
        }
    }

因此我必须:

1)在类(Person.Sports和Sports.Person)中创建集合,这意味着实际上有两个一对多的关系。

2)明确创建联结表

我不确定这是否属于正常做法(上述两种做法),并对一些评论表示感谢。