在 FluetnNhibernate 中映射自定义类型
我有2节课。
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Person
{
public virtual Name Name { get; set; }
public virtual int Age { get; set; }
}
我想将 Person 映射到数据库,如下所示:
| First Name | LastName | Age |
我尝试为 Name 创建 IUserType 实现。但
public SqlType[] SqlTypes
{
get { return new[] { new SqlType(DbType.String), new SqlType(DbType.String) }; }
}
我这里有一个例外
property mapping has wrong number of columns
I have 2 classes.
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Person
{
public virtual Name Name { get; set; }
public virtual int Age { get; set; }
}
I want to map Person to database like this:
| First Name | LastName | Age |
I tried to create IUserType implementation for Name. But here
public SqlType[] SqlTypes
{
get { return new[] { new SqlType(DbType.String), new SqlType(DbType.String) }; }
}
I have an exception
property mapping has wrong number of columns
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上要求的是一个组件:
https://github .com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#componentmap
您的类映射如下所示:
这会将 FirstName/LastName 作为两个单独的列映射到同一个人表。但是在您的 Person 上给您一个 Name 对象。
如果您需要 AutoMap 组件,请查看此博客:
https://web.archive.org/web/20190212181857/http://www.jagregory.com:80/writings/ Fluent-nhibernate-auto-mapping-components/
What you're actually asking for is a Component:
https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#componentmap
Your class maps would look like:
That will map the FirstName/LastName to the same person table as two separate columns. But give you a single Name object on your Person.
If you need to AutoMap the component, take a look at this blog:
https://web.archive.org/web/20190212181857/http://www.jagregory.com:80/writings/fluent-nhibernate-auto-mapping-components/