流利的nhibernate如何自动维护我的数据库中的外键?

时间:2012-10-25 22:52:52

标签: c# nhibernate fluent-nhibernate automapping

我有以下课程:

public class Report : Entity
{
    // Attributes
    public virtual string Name { get; set; }

    // Relationships
    public virtual ReportTemplate Template { get; set; }

    public virtual IList<Driver> Drivers { get; set; }
    public virtual IList<Vehicle> Vehicle { get; set; }
    public virtual IList<Address> Location { get; set; }

    public Report()
    {
        Drivers = new List<Driver>();
        Vehicle = new List<Vehicle>();
        Location = new List<Address>();
    }
}

public class Vehicle : Entity
{
    // Attributes
    public virtual string VehicleIdentificationNumber { get; set; }

    // Releationships
    public virtual Driver Driver { get; set; }
}

我正在使用nhibernate自动化来映射类(我正在转向流畅的映射,但现在只使用auto):

    public static ISessionFactory BuildSessionFactory()
    {
        AutoPersistenceModel model = CreateMappings();

        return Fluently.Configure()
            .Database(PostgreSQLConfiguration.Standard
                .ConnectionString("User ID=postgres;Password=1234;Host=localhost;Port=5432;Database=client;"))
            .Mappings(m => m
                .AutoMappings.Add(model))
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();
    }

    private static AutoPersistenceModel CreateMappings()
    {
        return AutoMap
            .Assembly(System.Reflection.Assembly.GetAssembly(typeof(Domain.Entity)))
            .Where(t => t.Namespace == "PSO_Persistence.Domain.Impl" ||
                t.Namespace == "PSO_Persistence.Domain.Impl.Template");
    }

一切都很好,我将车辆添加到报告中并保存。然后检索报告并查看它的车辆。

但是,当我查看数据库时,我发现它创建了以下内容:

create table "Report" (
    Id int4 not null,
   Name varchar(255),
   CreateDate timestamp,
   Template_id int4,
   primary key (Id)
)

create table "Vehicle" (
    Id int4 not null,
   VehicleIdentificationNumber varchar(255),
   CreateDate timestamp,
   Driver_id int4,
   Report_id int4,
   primary key (Id)
)

alter table "Vehicle" 
    add constraint FK425FBCB6B215E07F 
    foreign key (Report_id) 
    references "Report"

当我看车辆时,车辆的report_id,它总是空白的......但它显然以某种方式连接它。

有人可以向我解释一下,还是指出我的工作方式是什么?

0 个答案:

没有答案