简体   繁体   English

FluentNhibernate映射和结构

[英]FluentNhibernate mapping and struct

I tought that it's simple problem, just few minuts in gogle and I got the solution etc. But, actually, I found nothing special. 我坚信这是一个简单的问题,在gogle中只有很少的细节,我得到了解决方案等。但是,实际上,我发现没有什么特别的。

So I got a struct(or class whatever) and a class where every field got such struct type. 因此,我得到了一个struct(或任何类的类)和一个其中每个字段都具有这种struct类型的类。 And I want to make/use table in my database using model, where each field got such type as struct. 我想使用模型在数据库中创建/使用表,其中每个字段都具有struct这样的类型。

As example struct is here 作为示例struct在这里

public class myStruct
{
    public float value;
    public string description;
}

and here the model which I want to use 这里是我要使用的模型

public class FieldSummaryRow
{
    public int Id{ get; set } 
    public myStruct A { get; set; }
    public myStruct B { get; set; }
    public myStruct C { get; set; }
    public class FieldSummaryMap: ClassMap<FieldSummaryRow>
    {
      public FieldSummaryMap()
      {
        Id(x => x.Id);
        Map (x=>x.A);
        Map (x=>x.B);
        Map (x=>x.C);
      }
    }
}

But as I assume it just wouldn't work like that because of myStruct is not mapped correctly. 但是我认为由于myStruct映射不正确,它无法像那样工作。 So how should I solve this problem? 那么我该如何解决这个问题呢? Or do I nned map myStruct the same way before? 还是我myStruct以同样的方式映射myStruct

From NHibernate perspective, we should think about your struct as a <component> . 从NHibernate的角度来看,我们应该将您的结构视为<component> It means, that there must be columns valueA and descriptionA for myStruct A , the same for B and C . 这意味着, myStruct A必须有valueAdescriptionA列,而BC必须相同。

See the documentation: 请参阅文档:

Such a <component> myStruct would be at the end mapped like this xml snippet: 这样的<component> myStruct将像下面的xml片段一样被映射为末尾:

<class name="FieldSummaryRow" table="...>
    ....

    <component class="myStruct" name="A">
      <property name="value"       column="valueA"       access="field" />
      <property name="description" column="descriptionA" access="field" />
    </component>

    <component class="myStruct" name="B">
    ...

</class>

As we can see here (article by Adam Bar - see the second half as a great summary of Fluent mapping) 正如我们在这里看到的(Adam Bar的文章-将后半部分作为Fluent映射的摘要)

we would need this: 我们需要这个:

public class FieldSummaryMap: ClassMap<FieldSummaryRow>
{
    public FieldSummaryMap()
    {
        Id(x => x.Id);
        Component(x => x.A, c =>
        {        
            c.Map(x => x.value).Column("valueA").Access.CamelCaseField();
            c.Map(x => x.description).Column("descriptionA").Access.CamelCaseField();
        }
        Component(x => x.B, c =>
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM