【发布时间】:2016-08-02 09:30:38
【问题描述】:
我有一个数据库,它保存了一个项目的Id,该项目存储在另一个系统中并反序列化为代码中的一个对象。我正在尝试使用 Fluent NHibernate 构建一个由数据库和外部服务中的数据组成的域模型实体。一个例子将更好地解释这一点。在数据库中,我有一个如下所示的表:
CREATE TABLE entities
(
id integer NOT NULL,
custom_thing text NOT NULL,
CONSTRAINT entities_id_pk PRIMARY KEY (id)
);
它是 PostgreSQL,但问题不是特定于数据库的。现在在代码中我有这些类:
class Entity
{
public virtual int Id { get; set; }
public virtual CustomThing CustomThing { get; set; }
}
class CustomThing
{
public string Id { get; set; }
public string Name { get; set; }
}
我正在尝试使用CustomMap 来定义映射:
class EntityMap : ClassMap<Entity>
{
public EntityMap()
{
Table("entities");
Id(e => e.Id, "id").GeneratedBy.Assigned();
// Map(e => e.CustomThing, "custom_thing");
}
}
问题是:如何映射 CustomThing?这是一个尝试映射实体类的程序(如果针对 PostgreSQL 数据库运行,则需要 FluentNHibernate 包和 NpgSQL)。为简单起见,我只是在代码中创建 CustomThing 的实例:
class Program
{
// How to use this in mapping?
static CustomThing[] _customThings =
{
new CustomThing {Id = "abc", Name = "ABC"},
new CustomThing {Id = "def", Name = "DEF"}
};
static void Main()
{
using (ISessionFactory sessionFactory = Fluently.Configure()
.Database(PostgreSQLConfiguration.Standard
.ConnectionString(
@"Server=localhost; Database=test_nhibernate; User Id=postgres; Password=little_secret;"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entity>())
.BuildSessionFactory())
{
using (ISession session = sessionFactory.OpenSession())
{
var entity = session.Get<Entity>(1);
Console.WriteLine($"{entity?.Id} {entity?.CustomThing?.Name}");
}
}
Console.ReadLine();
}
}
输出当然只是 Id 属性值,因为未定义 CustomThing 的映射。是否可以配置映射,以便我可以将参数传递给它并以某种方式通过 Id 属性将 custom_thing 列值映射到 _customThings 中的对象?
【问题讨论】:
-
我解决了同样的问题,将这个属性存储为 json 文本并创建新的自定义 'UserType' 来序列化和反序列化对象,如果这个解决方案适合你,我可以为你提供一些代码 sn- ps?
-
@mdameer 谢谢,好的,我想我明白了这一点,但是您是如何指示映射器分配该 JSON 的呢?我假设您的意思是
Entity.CustomThing在这里是string类型。问题是在entities表中我不存储整个序列化CustomThing,而只存储它的Id。其他属性来自外部服务,它具有完整的CustomThingJSON 结构。您的解决方案在这种情况下是否有效?
标签: c# nhibernate fluent-nhibernate fluent-nhibernate-mapping