我有两张桌子和一根魔杖来创建一个联结表。我有这个映射。
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" ),
edit2 :(为了更好的观点)
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";
答案 0 :(得分:0)
我得出的结论是,为了在nHibernate中有一个复合主键,你必须使用另一个列来用于nHibernate更新数据库中的行。
此处更多内容: http://devlicio.us/blogs/anne_epstein/archive/2009/11/20/nhibernate-and-composite-keys.aspx
我相信因此,你必须在联结表中添加更多列(1列是预先确定的)以达到我想要的目的。
享受!