Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nhibernate:如何映射引用enity的组件?_Nhibernate_Fluent Nhibernate_Nhibernate Mapping - Fatal编程技术网

nhibernate:如何映射引用enity的组件?

nhibernate:如何映射引用enity的组件?,nhibernate,fluent-nhibernate,nhibernate-mapping,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,我的数据库看起来像这样: MyEntity State ----- ----- id id street name stateId ... zip status ... class MyEntity { int id { get; set; } Address location { get; set; } string status { get; set; } // ... } class

我的数据库看起来像这样:

MyEntity     State
-----        -----
id           id
street       name
stateId      ...
zip
status
...
class MyEntity
{
     int id { get; set; }
     Address location { get; set; }
     string status { get; set; }
     // ...
}

class Address
{
    string street { get; set; }
    string zip { get; set; }
    State state { get; set; }
    // ...
}

class State
{
    int id { get; set; }
    string name { get; set; }
    // ...
}
我的模型如下所示:

MyEntity     State
-----        -----
id           id
street       name
stateId      ...
zip
status
...
class MyEntity
{
     int id { get; set; }
     Address location { get; set; }
     string status { get; set; }
     // ...
}

class Address
{
    string street { get; set; }
    string zip { get; set; }
    State state { get; set; }
    // ...
}

class State
{
    int id { get; set; }
    string name { get; set; }
    // ...
}

我对我的地址组件引用实体有点不舒服。闻起来像个糟糕的模特。它是?如果没有,我将如何映射它(最好使用fluent nhibernate)?

我也不确定如何从组件引用实体。我自己也这样做过(与一个国家实体,同样如此)

就映射而言,它非常简单:

public class MyEntityMap : ClassMap<MyEntity>
{
    public MyEntityMap()
    {
        Id(x => x.id);
        Component<Address>(x => x.location, c =>
        {
            c.Map(x => x.street);
            c.Map(x => x.zip);
            c.References<State>(x => x.state);
        });
        Map(x => x.status);
    }
}
公共类MyEntityMap:ClassMap
{
公共MyEntityMap()
{
Id(x=>x.Id);
组件(x=>x.location,c=>
{
c、 地图(x=>x.street);
c、 Map(x=>x.zip);
c、 参考(x=>x.state);
});
Map(x=>x.status);
}
}
有时,我要做的是为组件添加一个静态类,以使类映射更好一些:

public static class NameMap
{
    public static Action<ComponentPart<Name>> AsComponent(string prefix)
    {
        return c =>
        {
            c.Map(x => x.Title, ColumnName(prefix, "Title"));
            // and so on
        };
    }
}
公共静态类名称映射
{
公共静态操作组件(字符串前缀)
{
返回c=>
{
c、 地图(x=>x.Title,ColumnName(前缀,“Title”);
//等等
};
}
}
在本例中,ColumnName是一个简单的函数,它将前缀附加到列名上(这在我即将使用的出色的遗留DBs中非常方便)

然后在类映射中,您只需执行以下操作:

Component<Name>(x => x.Name, c => NameMap.AsComponent("prefix"));
Component(x=>x.Name,c=>NameMap.AsComponent(“前缀”);

oo,我喜欢更流畅的组件语法!我不确定是否可以从组件引用。谢谢你证实我能。引用你的国家实体,然后解决?还是你最终改变了它?是的,它照样工作。在我看来,适当的模型是将地址作为一个单独的实体,在这种情况下,地址和状态之间有一个关系就可以了。然而,对于遗留DBs,我没有重组的选择,所以我必须关注可以做什么,而不是什么是“正确的”。