0

我有两个表,我想创建一个联结表。我有这个映射。

public UsertMap()
    {
        Table("Users");
        Id(x => x.Id, map => map.Generator(Generators.Assigned));
        Property(x => x.FirstName, map => map.NotNullable(true));
        Property(x => x.LastName, map => map.NotNullable(true));
        Property(x => x.Role, map => map.NotNullable(true));
        Bag(x => x.Projects, map =>
        {
            map.Table("UsersInProject");
            map.Cascade(Cascade.None);
            map.Access(Accessor.Field);
            map.Key(u => u.Column("UserId")); 
            }, a => a.ManyToMany(x => x.Column("ProjectId")));
    }

public ProjectMap()
    {
        Table("Projects");
        Id(x => x.Id, map => map.Generator(Generators.Assigned));
        Property(x => x.SubmissionDate, map =>map.NotNullable(true));
        Property(x => x.QuotesubmissionDate, map => map.NotNullable(true));
        Bag(x => x.ProjectUsers, map =>
        {
            map.Table("UsersInProject");
            map.Cascade(Cascade.None);
            map.Access(Accessor.Field);
            map.Key(u => u.Column("ProjectId"));
        }, a => a.ManyToMany(x => x.Column("UserId")));
    }

它创建了联结表,但没有设置为主键*,复合键 {ProjectId , UserId}

我究竟做错了什么?

提前谢谢。

*当我被告知有关我的代码时,我也很奇怪地理解它。创建的表是由这个 sql 代码创建的:

CREATE TABLE "UsersInProject"
(
  "ProjectId" uuid NOT NULL,
  "UserId" uuid NOT NULL,
  CONSTRAINT fk28998796db607aa2 FOREIGN KEY ("UserId")
      REFERENCES "Users" ("Id") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk28998796f9a8d344 FOREIGN KEY ("ProjectId")
      REFERENCES "Projects" ("Id") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "UsersInProject"
  OWNER TO test;

我也希望像下面这样一行:

CONSTRAINT pk PRIMARY KEY ("ProjectId" , "UserId" ),

编辑2:(为了更好的视图)

drop table if exists "UsersInProject" cascade;
create table "UsersInProject" (
        "ProjectId" uuid not null,
       "UserId" uuid not null
    );
    alter table "UsersInProject" 
        add constraint FK28998796DB607AA2 
        foreign key ("UserId") 
        references "Users";

    alter table "UsersInProject" 
        add constraint FK28998796F9A8D344 
        foreign key ("ProjectId") 
        references "Projects";
4

1 回答 1

0

我得出的结论是,为了在 nHibernate 中拥有复合主键,您必须使用另一列用于 nHibernate 更新数据库中的行。

更多内容在这里: http ://devlicio.us/blogs/anne_epstein/archive/2009/11/20/nhibernate-and-composite-keys.aspx

我相信正因为如此,您必须在联结表中添加更多列(精确的 1 列)才能实现我想要的。

享受!

于 2012-12-07T13:00:57.190 回答