帮助 Fluent NHibernate 映射通用类

发布于 2024-09-06 00:37:10 字数 2269 浏览 3 评论 0 原文

我希望有人可以帮助我解决这个问题,我正在尝试为子类化为泛型类的类编写映射类。用代码描述更容易,所以这是我的模型...

public abstract class TagBase
{
    public virtual int Id { get; private set; }
    public virtual TagTypeEnum TagType { get; set; }
    public virtual string Value { get; set; }
    public virtual bool IsSystemTag { get; private set; }
    public virtual bool isDeleted { get; set; }

    public TagBase()
    {
    }

    public TagBase(TagTypeEnum tagType)
    {
        this.TagType = tagType;
    }

    public override string ToString()
    {
        return Value;
    }
}

public class Tag<TLinkedItem> : TagBase where TLinkedItem : ITagged<TLinkedItem>
{
    public virtual List<TLinkedItem> LinkedItems { get; set; }
}

您所见 Tag 是通用的,但仅限于那些实现 ITagged 的类

如 想法是实体具有标签,而标签又具有强类型的 LinkedItem 列表。

标签不能在不同类型的实体之间共享,

到目前为止,我的模型中只有两个实体支持标记,即 DisplayAsset

我尝试将其映射到遵循 Fluent NHibernate 代码;

public class TagBaseMap : ClassMap<TagBase>
{
    public TagBaseMap()
    {
        Table("Tag");
        Id(tag => tag.Id);
        Map(tag => tag.Value).Not.Nullable().Length(50);
        Map(tag => tag.IsSystemTag).Not.Nullable();
        Map(tag => tag.isDeleted).Not.Nullable();
        Map(tag => tag.TagType).CustomType<Int32>().Not.Nullable();
    }
}

public class DisplayTagMap : SubclassMap<Tag<Display>>
{
    public DisplayTagMap()
    {
        HasManyToMany(displayTag => displayTag.LinkedItems).Inverse().Table("DisplayTagLink");
    }
}

public class AssetTagMap : SubclassMap<Tag<Asset>>
{
    public AssetTagMap()
    {
        HasManyToMany(assetTag => assetTag.LinkedItems).Inverse().Table("AssetTagLink");
    }
}

当我尝试构建数据库时,我的目标是 Sql Server,从这个映射中我收到此错误 “'`' 附近的语法不正确。”

我的想法是否正确,是由 .Net 的命名引起的通用类,即

'{[Model.Tag'1[[Model.Asset, Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].LinkedItems, NHibernate.Mapping。 Bag(Model.Tag'1[[Model.Asset, Saturn.ConnectVision.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].LinkedItems)]}'

我在做什么这里有什么愚蠢的东西吗?

有办法解决这个问题吗?

预先感谢您的任何建议。

I'm hoping somebody can help me with this, I'm trying to write the mapping classes for a class that is subclassed into a generic class. Its easier to descibe with code, so here is my model...

public abstract class TagBase
{
    public virtual int Id { get; private set; }
    public virtual TagTypeEnum TagType { get; set; }
    public virtual string Value { get; set; }
    public virtual bool IsSystemTag { get; private set; }
    public virtual bool isDeleted { get; set; }

    public TagBase()
    {
    }

    public TagBase(TagTypeEnum tagType)
    {
        this.TagType = tagType;
    }

    public override string ToString()
    {
        return Value;
    }
}

public class Tag<TLinkedItem> : TagBase where TLinkedItem : ITagged<TLinkedItem>
{
    public virtual List<TLinkedItem> LinkedItems { get; set; }
}

As you can see Tag is generic but is constrained to only those classes that implement ITagged<T>

The idea is that an entity has Tags and in turn a tag has a list of LinkedItems that are strongly typed.

Tags cannot be shared amongst different types of entity

I only have two entities in my model that support tagging so far thay are Display and Asset

I've tried to map this with the following Fluent NHibernate code;

public class TagBaseMap : ClassMap<TagBase>
{
    public TagBaseMap()
    {
        Table("Tag");
        Id(tag => tag.Id);
        Map(tag => tag.Value).Not.Nullable().Length(50);
        Map(tag => tag.IsSystemTag).Not.Nullable();
        Map(tag => tag.isDeleted).Not.Nullable();
        Map(tag => tag.TagType).CustomType<Int32>().Not.Nullable();
    }
}

public class DisplayTagMap : SubclassMap<Tag<Display>>
{
    public DisplayTagMap()
    {
        HasManyToMany(displayTag => displayTag.LinkedItems).Inverse().Table("DisplayTagLink");
    }
}

public class AssetTagMap : SubclassMap<Tag<Asset>>
{
    public AssetTagMap()
    {
        HasManyToMany(assetTag => assetTag.LinkedItems).Inverse().Table("AssetTagLink");
    }
}

When I try and build the database, I'm targetting Sql Server, from this mapping I'm getting this error "Incorrect syntax near '`'."

Am I right in thinking caused by .Net's naming of generic classes ie,

'{[Model.Tag'1[[Model.Asset, Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].LinkedItems, NHibernate.Mapping.Bag(Model.Tag'1[[Model.Asset, Saturn.ConnectVision.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].LinkedItems)]}'

Am I doing something daft here?

Is there a way around this problem?

Thanks in advance for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-09-13 00:37:10

好吧,我现在似乎已经解决了这个问题。

这是 Tag where T : ITagged 的​​通用版本的映射,

public class DisplayTagMap : SubclassMap<Tag<Display>>
{
    public DisplayTagMap()
    {
       HasManyToMany(displayTag => displayTag.LinkedItems).Inverse().Table("DisplayTagLink").ParentKeyColumn("display_id").ChildKeyColumn("tag_id");
    }
}

public class AssetTagMap : SubclassMap<Tag<Asset>>
{
    public AssetTagMap()
    {
        HasManyToMany(assetTag => assetTag.LinkedItems).Inverse().Table("AssetTagLink").ParentKeyColumn("asset_id").ChildKeyColumn("tag_id");
    }
}

因此我将覆盖导致非法 Sql 语法的生成列名称,当然还需要考虑反向映射。

    public class DisplayMap : ClassMap<Display>
{
    public DisplayMap()
    {
        Id(display => display.Id);
        Map(display => display.Name).Not.Nullable();
        Map(display => display.DisplayGUID).Not.Nullable();
        Map(display => display.Description);
        HasManyToMany(display => display.Tags).Cascade.All().Table("DisplayTagLink").ParentKeyColumn("display_id").ChildKeyColumn("tag_id");
    }
}

我现在遇到的唯一问题是,如果我放入一个 DiscriminateSubclassesOnColumn("TagType") 我会遇到相同的语法问题。

如果我错过了这个声明,我最终会得到两个额外的表, Tag_Asset 和 Tag_Display 仅包含返回 Tag 的外键。目前这不是什么大问题。

我避免使用 DiscriminateSubClassOnColumn("TagType").SubClass>(..... 因为这将导致我必须为我添加的每个子类修改此声明。

OK, I seemed to have worked around this problem for now.

Here is the mapping for the generic versions of Tag where T : ITagged

public class DisplayTagMap : SubclassMap<Tag<Display>>
{
    public DisplayTagMap()
    {
       HasManyToMany(displayTag => displayTag.LinkedItems).Inverse().Table("DisplayTagLink").ParentKeyColumn("display_id").ChildKeyColumn("tag_id");
    }
}

public class AssetTagMap : SubclassMap<Tag<Asset>>
{
    public AssetTagMap()
    {
        HasManyToMany(assetTag => assetTag.LinkedItems).Inverse().Table("AssetTagLink").ParentKeyColumn("asset_id").ChildKeyColumn("tag_id");
    }
}

So I'm overriding the generated column names that cause illegal Sql syntax, of course there is the reverse mapping to consider.

    public class DisplayMap : ClassMap<Display>
{
    public DisplayMap()
    {
        Id(display => display.Id);
        Map(display => display.Name).Not.Nullable();
        Map(display => display.DisplayGUID).Not.Nullable();
        Map(display => display.Description);
        HasManyToMany(display => display.Tags).Cascade.All().Table("DisplayTagLink").ParentKeyColumn("display_id").ChildKeyColumn("tag_id");
    }
}

The only issue I now have is that if I put in a DiscriminateSubclassesOnColumn("TagType") I get the same syntax issue creeping in.

If I miss out this declaration, I end up with two additional tables, Tag_Asset and Tag_Display that just contain a foreign key back to Tag. This is not such a big deal for now.

I've avoided DiscriminateSubClassOnColumn("TagType").SubClass<Tag<Display>>(..... because this will cause me to have to modify this declaration for each subclass that I add.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文