Fluent Automapper 标签创建问题

发布于 2024-12-06 17:08:40 字数 2575 浏览 0 评论 0原文

已编辑的帖子 - 请参阅下面的编辑

我对用作 SHarp 架构一部分的 FLuent 自动映射有疑问。运行其中一个测试用例将生成一个架构,我可以使用该架构在数据库中创建表。

我正在开发一个包含帖子以及与这些帖子相关的标签的网站。我希望一个标签能够与多个帖子关联,并且每个帖子都有 0 个或多个标签。

我想实现以下数据库模式:

Post {Id,Title,SubmitTime,Content}

Tag {Id,Name}

PostTag {PostId,TagId}

相反,我得到:

Post {Id,Title,SubmitTime,Content}

Tag { Id、Name、PostID (FK)}

我正在使用清晰的架构,并且类可能如下所示(或多或少):

public class Post : Entity
{
    [DomainSignature]
    private DateTime _submittime;
    [DomainSignature]
    private String _posttitle;

    private IList<Tag> _taglist;

    private String _content;

    public Post() { }

    public Post(String postTitle)
    {
        _submittime = DateTime.Now;
        _posttitle = postTitle;
        this._taglist = new List<Tag>();
    }


    public virtual DateTime SubmitTime { get { return _submittime; } private set { _submittime = value; } }

    public virtual string PostTitle { get { return _posttitle; } private set { _posttitle = value; } }

    public virtual string Content { get { return _content; } set { _content = value; } }

    public virtual IList<Tag> TagList { get { return _taglist; }  set { _taglist = value; } }




 public class Tag : Entity
{
    [DomainSignature]
    private String _name;

    public Tag() { }

    public Tag(String name)
    {
        this._name = name;
    }

    public virtual String Name
    {
        get { return _name; }
        private set { _name = value; }
    }

    public virtual void EditTagName(String name)
    {
        this.Name = name;
    }

}

我可以理解为什么它消失了它所设置的数据库模式,因为有时会出现一个对象只能作为另一个的一部分。但标签可以单独存在。

我将如何实现这一目标?我对 MVC、Nhibernate 和 SHARP 架构等还很陌生,所以任何帮助将不胜感激!

编辑*

好的,我现在已经稍微调整了我的课程。我的问题是我期望推断出中间表。相反,我意识到我必须创造它。 所以我现在有了(为了可读性,我对类进行了一些简化。:

class Post : Entity
{
 [DomainSignature]
 String Title
 [DomainSignature]
 DateTime SubmitTime
 IList<PostTag> tagList

}

class Tag : Entity
{
[DomainSignature]
string name
}

class PostTag : Entity
{
[DomainSignature]
Post post
[DomainSignature]
Tag tag
}

这为我提供了中间实体的模式以及通常的帖子和标签表:

PostTag{id, name, PostId(FK)}

上面的问题是它仍然不包括外部实体 认为它实际上应该是一个由 Post 和 Tag 表中的 PK 组成的复合键。

另外,它真的应该有一个 ID 列吗?因为它是一个关系表,我 对于 Tag 类,

IList<PostTag> postList

我将添加另一个 FK PostTag 架构,但我不想添加上述内容,因为每次将帖子带入系统时我都不需要它,因为我需要一个单独的查询来计算此类信息。 ?

任何人都可以帮我解决最后一部分吗

POST EDITED - see edit below

I have a query about the FLuent Automapping which is used as part of the SHarp Architecture. Running one of the tests cases will generate a schema which I can use to create tables in my DB.

I'm developing a site with Posts, and Tags associated with these posts. I want a tag to be able to be associated with more than one post, and for each post to have 0 or more tags.

I wanting to achieve a DB schema of:

Post {Id, Title, SubmitTime, Content}

Tag {Id, Name}

PostTag {PostId, TagId}

Instead, I'm getting:

Post {Id, Title, SubmitTime, Content}

Tag {Id, Name, PostID (FK)}

I'm using sharp architecture, and may classes look as follows (more or less):

public class Post : Entity
{
    [DomainSignature]
    private DateTime _submittime;
    [DomainSignature]
    private String _posttitle;

    private IList<Tag> _taglist;

    private String _content;

    public Post() { }

    public Post(String postTitle)
    {
        _submittime = DateTime.Now;
        _posttitle = postTitle;
        this._taglist = new List<Tag>();
    }


    public virtual DateTime SubmitTime { get { return _submittime; } private set { _submittime = value; } }

    public virtual string PostTitle { get { return _posttitle; } private set { _posttitle = value; } }

    public virtual string Content { get { return _content; } set { _content = value; } }

    public virtual IList<Tag> TagList { get { return _taglist; }  set { _taglist = value; } }




 public class Tag : Entity
{
    [DomainSignature]
    private String _name;

    public Tag() { }

    public Tag(String name)
    {
        this._name = name;
    }

    public virtual String Name
    {
        get { return _name; }
        private set { _name = value; }
    }

    public virtual void EditTagName(String name)
    {
        this.Name = name;
    }

}

I can see why it's gone for the DB schema set up that it has, as there will be times when an object can only exist as part of another. But a Tag can exist separately.

How would I go about achieving this? I'm quite new to MVC, Nhibernate, and SHarp architecture, etc, so any help would be much appreciated!

EDIT*

OK, I have now adjusted my classes slightly. My issue was that I was expecting the intermediate table to be inferred. Instead, I realise that I have to create it.
So I now have (I've simplified the classes a bit for readability's sake.:

class Post : Entity
{
 [DomainSignature]
 String Title
 [DomainSignature]
 DateTime SubmitTime
 IList<PostTag> tagList

}

class Tag : Entity
{
[DomainSignature]
string name
}

class PostTag : Entity
{
[DomainSignature]
Post post
[DomainSignature]
Tag tag
}

This gives me the schema for the intermediate entity along with the usual Post and Tag tables:

PostTag{id, name, PostId(FK)}

The problem with the above is that it still does not include The foreign key for Tag. Also, should it really have an ID column, as it is a relational table? I would think that it should really be a composite key consisting of the PK from both Post and Tag tables.

I'm sure that by adding to the Tag class

IList<PostTag> postList

I will get another FK added to the PostTag schema, but I don't want to add the above, as the postList could be huge. I don't need it every time I bring a post into the system. I would have a separate query to calculate that sort of info.

Can anyone help me solve this last part? Thanks for your time.

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

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

发布评论

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

评论(1

始终不够 2024-12-13 17:08:40

好吧,我被引导相信在域中对复合类进行建模是前进的方向,但我最终遇到了一些自动映射器重写代码,它创建了复合表,而无需我为其创建类,这是我首先期待的是:

公共类 PostMappingOverride
:IAutoMappingOverride
{
公共无效覆盖(自动映射地图)
{

            map.HasManyToMany(e => e.TagList)
               .Inverse()
               .Cascade.SaveUpdate();
        }
    }

现在这将给我我的模式(以下模式非简化):

create table Posts (
    Id INT not null,
   PublishTime DATETIME null,
   SubmitTime DATETIME null,
   PostTitle NVARCHAR(255) null,
   Content NVARCHAR(255) null,
   primary key (Id)
)

create table Posts_Tags (
    PostFk INT not null,
   TagFk INT not null
)

create table Tags (
    Id INT not null,
   Name NVARCHAR(255) null,
   primary key (Id)
)

alter table Posts_Tags 
    add constraint FK864F92C27E2C4FCD 
    foreign key (TagFk) 
    references Tags

alter table Posts_Tags 
    add constraint FK864F92C2EC575AE6 
    foreign key (PostFk) 
    references Posts

我认为抛出者是我一直在寻找一对多关系,确实如此,但这里称为 HasManytoMAny...

Ok, I'd been led to believe that modelling the composite class in the domain was the way forward, but I finally come across a bit of automapper override code which creates the composite table without me needing to create the class for it, which was what I was expecting in the first place:

public class PostMappingOverride
: IAutoMappingOverride
{
public void Override(AutoMapping map)
{

            map.HasManyToMany(e => e.TagList)
               .Inverse()
               .Cascade.SaveUpdate();
        }
    }

This will now give me my schema (following schema non simplified):

create table Posts (
    Id INT not null,
   PublishTime DATETIME null,
   SubmitTime DATETIME null,
   PostTitle NVARCHAR(255) null,
   Content NVARCHAR(255) null,
   primary key (Id)
)

create table Posts_Tags (
    PostFk INT not null,
   TagFk INT not null
)

create table Tags (
    Id INT not null,
   Name NVARCHAR(255) null,
   primary key (Id)
)

alter table Posts_Tags 
    add constraint FK864F92C27E2C4FCD 
    foreign key (TagFk) 
    references Tags

alter table Posts_Tags 
    add constraint FK864F92C2EC575AE6 
    foreign key (PostFk) 
    references Posts

I think the thrower is that I've been looking for a one-to-many relationship, which it is, but it is called HasManytoMAny here...

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