我使用FluentNHibernate(v1.0.0.637)定义了多对多映射(仅包含相关字段):
// MODEL:
public class User : IPersistentObject {
public User() {
Permissions = new HashedSet<Permission>();
}
public virtual int Id { get; protected set; }
public virtual ISet<Permission> Permissions { get; set; }
}
public class Permission : IPersistentObject {
public Permission() {
}
public virtual int Id { get; set; }
}
// MAPPING:
public class UserMap : ClassMap<User> {
public UserMap() {
Id(x => x.Id);
HasManyToMany(x => x.Permissions).Cascade.All().AsSet();
}
}
public class PermissionMap : ClassMap<Permission> {
public PermissionMap() {
Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Description);
}
}
以下测试失败,因为在User_Permission表中插入了无记录:
[Test]
public void AddingANewUserPrivilegeShouldSaveIt() {
var p1 = new Permission {
Id = 123,
Description = "p1"
};
Session.Save(p1);
var u = new User {
Email = "bla@here.there"
};
u.Permissions.Add(p1);
Session.Save(u);
var userId = u.Id;
Session.Evict(u);
Session.Get<User>(userId).Permissions.Should().Not.Be.Empty();
}
执行的SQL是(SQLite):
INSERT INTO "Permission" (Description, Id) VALUES (@p0, @p1);@p0 = 'p1', @p1 = 1
INSERT INTO "User" (Email) VALUES (@p0); select last_insert_rowid();@p0 = 'bla@here.there'
SELECT user0_.Id as Id2_0_, user0_.Email as Email2_0_ FROM "User" user0_ WHERE user0_.Id=@p0;@p0 = 1
SELECT permission0_.UserId as UserId1_, permission0_.PermissionId as Permissi2_1_, permission1_.Id as Id4_0_, permission1_.Description as Descript2_4_0_ FROM User_Permissions permission0_ left outer join "Permission" permission1_ on permission0_.PermissionId=permission1_.Id WHERE permission0_.UserId=@p0;@p0 = 1
我们可以清楚地看到它应该的User_Permissions
表格中插入无记录。
不确定我做错了什么并且需要建议。
那么请你帮我通过这个测试。
谢谢,
德米特里。
答案 0 :(得分:0)
正如miensol在评论中所说,我忘记了交易。