简体   繁体   English

nhibernate通过代码方式映射,映射对象

[英]nhibernate mapping by code approach, mapping object

I have following model 我有以下型号

public class Car : Entity<int>
{
    public virtual int Id{ get; set; }
    ...
    public virtual Engine Engine { get; set; }

}

and I'm using nhibernate mapping by code approach 我正在nhibernate mapping by code approach使用nhibernate mapping by code approach

public class CarMap : ClassMapping<Car>
{
    public CarMap()
    {
        Id(x => CarId, m => m.Generator(Generators.Identity));
        // how map reference Engine?  
        **// edit**
        HasOne(x=>x.Engine, m=>{}) // is this good enough?

    }
}

how map Engine in this CarMap object? 如何在此CarMap对象中映射引擎?

You need a little more information in the question, but here's a couple of options. 您需要更多有关该问题的信息,但是这里有几个选择。

Is this really a one to one relationship? 这真的是一对一的关系吗? One to one relationships are somewhat unique in that both sides of the relationship tend to share the same Id. 一对一关系在某种程度上是唯一的,因为关系的两端倾向于共享相同的ID。 Like David Osborne said you most likely want a One to Many relationship. 就像大卫·奥斯本(David Osborne)所说的那样,您很可能想要一对多的关系。 But you you want it to be bi-directional? 但是您希望它是双向的吗? ie you can navigate down from the Engine to all the cars that may have that engine or up from the car to a specific engine. 即,您可以从引擎向下导航到可能具有该引擎的所有汽车,或者从汽车向上导航至特定的引擎。 ie engine is Chrysler Hemi engine 5.7L and it is in the cars, Ram Pickup, Dodge Durango, Dodge Charger. 例如,发动机是克莱斯勒Hemi 5.7L发动机,并且在汽车,Ram Pickup,道奇杜兰戈,道奇Charger中使用。

Then you may want to map the objects like this 然后,您可能想要像这样映射对象

public class Engine : Entity<int>
{
    public Engine()
    {
        Cars = new List<Car>();
    }

    public virtual int Id { get; protected set; }
    public virtual decimal Displacement { get; set; }
    //more properties

    public virtual IList<Car> Cars { get; }

    public virtual void AddCar(Car car)
    {
        if (Cars.Contains(car)) return;

        Cars.Add(car);
    }

    public virtual void RemoveCar(Car car)
    {
        if (!Cars.Contains(car)) return;

        Cars.Remove(car);
    }
}

public class Car : Entity<int>
{
    public virtual int Id { get; set; }
    public virtual Engine Engine { get; set; }

}

So if you are mapping the Engine you need to define the Cars mapping list this 因此,如果要映射引擎,则需要定义“汽车映射”列表

        Bag(x => x.Cars, map =>
        {
            map.Key(k => k.Column(col => col.Name("EngineId")));
            map.Cascade(Cascade.All | Cascade.DeleteOrphans); //optional
        },
            action => action.OneToMany());

and the other side of the relationship like this 和关系的另一面

        ManyToOne(x => x.Engine, map =>
        {
            map.Column("EngineId");
            map.NotNullable(true); // if you require the Engine to be in a car
        });

If you just want a one way mapping from the car to the engine, just remove all the references to the Cars list and delete the Bag mapping. 如果只想从汽车到引擎的单向映射,只需删除对“汽车”列表的所有引用,然后删除“行李”映射即可。

使用Fluent NH,我将使用References() ,它显然具有等效ManyToOne() 的按代码映射

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

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