NHibernate 中级 table 按代码映射
NHibernate intermediate table mapping by code
我知道在 Whosebug 上有与此主题类似的问题,但其中 none 可以解决我的问题。我现在坚持了至少两天。所以这是我的问题:
我有两个 table 有 Many-To-Many
关系,像这样:
Department * - * Person
我不想使用 Many-To-Many
关系,而是两个 Many-To-One
并将 link table 定义为实体。
link table 应该叫 "Lam".
我的实体
public class Department
{
public virtual int Id { get; set; }
public virtual IList<Lam> Lams { get; set; }
}
public class Person
{
public virtual int Id { get; set; }
public virtual IList<Lam> Lams { get; set; }
}
public class Lam
{
public virtual Department Department { get; set; }
public virtual Person Person { get; set; }
}
我的映射
public class DepartmentMapping : ClassMapping<Department>
{
public DepartmentMapping()
{
Id(x => x.Id, map => map.Generator(Generators.Native));
Bag(x => x.Lams, col =>
{
col.Key(k => k.Column("DepartmentId"));
col.Inverse(true);
}, r => r.OneToMany());
}
}
public class PersonMapping : ClassMapping<Person>
{
public PersonMapping()
{
Id(x => x.Id, map => map.Generator(Generators.Native));
Bag(x => x.Lams, col =>
{
col.Key(k => k.Column("PersonId"));
col.Inverse(true);
}, r => r.OneToMany());
}
}
配对映射:
public class LamMapping : ClassMapping<Lam>
{
public LamMapping()
{
ManyToOne(x => x.Department, map =>
{
map.Column("DepartmentId");
});
ManyToOne(x => x.Person, map =>
{
map.Column("PersonId");
});
}
}
如果我尝试 运行 我的应用程序,我收到以下错误消息:
Incorrect syntax near 'Index'. If this is intended as a part of a
table hint, A WITH keyword and parenthesis are now required. See SQL
Server Books Online for proper syntax.
有人可以告诉我,我的代码有什么问题吗?
映射原样是正确的(至少对我来说是一样的)。所以,问题是你的例外是从哪里来的?
让我们SQLSELECT查询您的简化table/entity人:
SELECT id FROM Person
那行得通。
但是如果 - 在你的映射的未显示部分 - 存在人的一些 属性,假设 Index
像这样:
// entity
public class Person
{
...
// C# property named Index
public virtual int Index { get; set; }
...
// mapping
public class PersonMapping : ClassMapping<Person>
{
public PersonMapping()
{
...
// that by default would be column Index
Property(x => x.Index)
这会导致 SELECT 像这样
SELECT id, Index FROM Person
而在 SQL 服务器世界中,这将导致错误:
Msg 1018, Level 15, State 1, Line 1
Incorrect syntax near 'Index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required.
所以,因为你上面显示的映射是正确的,所以我怀疑有这样的部分
如果是这种情况,我们可以使用
...
Property(x => x.Index, x => { x.Column("[Index]"); });
我知道在 Whosebug 上有与此主题类似的问题,但其中 none 可以解决我的问题。我现在坚持了至少两天。所以这是我的问题:
我有两个 table 有 Many-To-Many
关系,像这样:
Department * - * Person
我不想使用 Many-To-Many
关系,而是两个 Many-To-One
并将 link table 定义为实体。
link table 应该叫 "Lam".
我的实体
public class Department
{
public virtual int Id { get; set; }
public virtual IList<Lam> Lams { get; set; }
}
public class Person
{
public virtual int Id { get; set; }
public virtual IList<Lam> Lams { get; set; }
}
public class Lam
{
public virtual Department Department { get; set; }
public virtual Person Person { get; set; }
}
我的映射
public class DepartmentMapping : ClassMapping<Department>
{
public DepartmentMapping()
{
Id(x => x.Id, map => map.Generator(Generators.Native));
Bag(x => x.Lams, col =>
{
col.Key(k => k.Column("DepartmentId"));
col.Inverse(true);
}, r => r.OneToMany());
}
}
public class PersonMapping : ClassMapping<Person>
{
public PersonMapping()
{
Id(x => x.Id, map => map.Generator(Generators.Native));
Bag(x => x.Lams, col =>
{
col.Key(k => k.Column("PersonId"));
col.Inverse(true);
}, r => r.OneToMany());
}
}
配对映射:
public class LamMapping : ClassMapping<Lam>
{
public LamMapping()
{
ManyToOne(x => x.Department, map =>
{
map.Column("DepartmentId");
});
ManyToOne(x => x.Person, map =>
{
map.Column("PersonId");
});
}
}
如果我尝试 运行 我的应用程序,我收到以下错误消息:
Incorrect syntax near 'Index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.
有人可以告诉我,我的代码有什么问题吗?
映射原样是正确的(至少对我来说是一样的)。所以,问题是你的例外是从哪里来的?
让我们SQLSELECT查询您的简化table/entity人:
SELECT id FROM Person
那行得通。
但是如果 - 在你的映射的未显示部分 - 存在人的一些 属性,假设 Index
像这样:
// entity
public class Person
{
...
// C# property named Index
public virtual int Index { get; set; }
...
// mapping
public class PersonMapping : ClassMapping<Person>
{
public PersonMapping()
{
...
// that by default would be column Index
Property(x => x.Index)
这会导致 SELECT 像这样
SELECT id, Index FROM Person
而在 SQL 服务器世界中,这将导致错误:
Msg 1018, Level 15, State 1, Line 1 Incorrect syntax near 'Index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required.
所以,因为你上面显示的映射是正确的,所以我怀疑有这样的部分
如果是这种情况,我们可以使用
...
Property(x => x.Index, x => { x.Column("[Index]"); });