Orchard CMS ISessionConfigurationEvents和1:N,N:N关系?

时间:2015-07-14 07:52:03

标签: nhibernate fluent-nhibernate many-to-many orchardcms one-to-many

我花了很多天试图在我的自定义内容组件之间实现OrchardCMS 1.9.1之间的关系无济于事。 遍布互联网的是许多其他人试图实现同样的事情,他们也失败了;给我的印象是不可能的? 虽然最近我在http://www.ideliverable.com/blog/isessionconfigurationevents阅读了一篇文章,但给人的印象是,在乌节之内,所有可能的Fluent Nhibernate都是可能的。

所以我实施了:

public class DbMapping : ISessionConfigurationEvents
{
    public void Created(FluentConfiguration cfg, AutoPersistenceModel defaultModel)
    {
        defaultModel.UseOverridesFromAssemblyOf<ProfilePartRecord>().Alterations(x => x.AddFromAssemblyOf<ProfileOverride>());
        defaultModel.UseOverridesFromAssemblyOf<LocationPartRecord>().Alterations(x => x.AddFromAssemblyOf<LocationOverride>());
    }

    public void Prepared(FluentConfiguration cfg) { }
    public void Building(Configuration cfg) { }
    public void Finished(Configuration cfg) { }
    public void ComputingHash(Hash hash) { }
}



public class LocationOverride : IAutoMappingOverride<LocationPartRecord>
{
    public void Override(AutoMapping<LocationPartRecord> mapping)
    {
        //[ Profile ] <--> [ Location ]
        //mapping.Id(x => x.Id, "LocationPartRecord_id"); //As it's not in the model due to being a contentPart, NH will throw an error because of such.
        mapping.Map(x => x.Type);
        mapping.Map(x => x.Name);
        mapping.References(x => x.ProfilePartRecord, "ProfilePartRecord_id");
    }
}


public class ProfileOverride : IAutoMappingOverride<ProfilePartRecord>
{
    public void Override(AutoMapping<ProfilePartRecord> mapping)
    {
        //[ Profile ] 0.1 <---> N [ Location ]
        //NEW
        mapping.HasMany(x => x.Locations)
            .Inverse()
            //.KeyColumn("ProfilePartRecord_id")
            .Cascade.All()
            .ForeignKeyCascadeOnDelete() 
            .ForeignKeyConstraintName("FK_Location__Profile"); 
    }
}

模型:

public class ProfilePartRecord : ContentPartRecord
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

    [CascadeAllDeleteOrphan]
    public virtual IList<LocationPartRecord> Locations { get; set; }
    public ProfilePartRecord()
    {
        Locations = new List<LocationPartRecord>();
    }
}


public class LocationPartRecord : ContentPartRecord
{
    public virtual string Type { get; set; }
    public virtual string Name { get; set; }

    //For HasMany
    [CascadeAllDeleteOrphan]
    public virtual ProfilePartRecord ProfilePartRecord { get; set; }
}

迁移:

        SchemaBuilder.CreateTable("ProfilePartRecord",
            table => table
                .ContentPartRecord()
                //PK: ProfilePartRecord_id
                .Column<string>("FirstName")
                .Column<string>("LastName")
                //System
                .Column<DateTime>("CreatedAt")
            );

        ContentDefinitionManager.AlterPartDefinition("ProfilePart",
            builder => builder.Attachable());

        ContentDefinitionManager.AlterTypeDefinition("Profile", t => t
            .WithPart(typeof(ProfilePart).Name)
            .WithPart("UserPart")
            );

        ContentDefinitionManager.AlterTypeDefinition("User", t => t
            .WithPart("ProfilePart")
            );



        SchemaBuilder.CreateTable("LocationPartRecord",
            table => table
                .ContentPartRecord()
                //PK: LocationPartRecord_id
                //FK:
                .Column<int>("ProfilePartRecord_id")
                .Column<string>("Type")
                .Column<string>("Name")
                //System
                .Column<DateTime>("CreatedAt")
            );

        ContentDefinitionManager.AlterPartDefinition("LocationPart",
            builder => builder.Attachable());

        ContentDefinitionManager.AlterTypeDefinition("Location", type => type
            .WithPart("CommonPart")
            .WithPart("LocationPart")
            .Creatable()
            .Listable());

但是,我仍然无法在这两个实体之间建立关系。我可以通过迁移来做到这一点,但这是非常有限的 - 因为 - 我无法设置与Cascade的关系。

任何人都可以了解这是否可能,如果可行,怎么样?感谢

0 个答案:

没有答案