领域建模问题

时间:2010-08-01 22:07:54

标签: c# domain-driven-design entity-framework-4 code-first

我正在处理的网站具有用户和网站的概念。用户可以加入全球网站并在(www.globalsite.com/minisite)内创建迷你网站。用户还可以关注其他用户创建的其他迷你网站。考虑一下,如果Twitter允许您加入一组凭据并创建不同的帐户,一个用于@personal使用,@ business使用等。

我正在使用EF CTP4并首先使用代码。我只是不确定我是否正确设计我的模型。

public class User : IEntity
{
    public virtual ICollection<Site> Sites{ get; set; }
    public virtual ICollection<Site> Following{ get; set; }
    ....
}

public class Site: IEntity
{
    public virtual User User{ get; set; }
    public virtual ICollection<User> Following{ get; set; }
    ....
}

这是我的映射:

public class UserMap : EntityConfiguration<User>
    {
        public UserMap()
        {
            HasKey(u => u.Id);
            Property(u => u.Id).IsIdentity();
            Property(u => u.Name).IsRequired().IsVariableLength().HasMaxLength(75);
            Property(u => u.Email).IsRequired().IsVariableLength().HasMaxLength(75);
            Property(u => u.PasswordHash).IsRequired().IsVariableLength().HasMaxLength(215);
            Property(u => u.PasswordSalt).IsRequired().IsVariableLength().HasMaxLength(88);
            Property(u => u.IsConfirmed);
            Property(u => u.LastActivityAt);
            Property(u => u.CreatedAt);
            Property(u => u.InternalRole);
            MapSingleType(u => new
                                   {
                                       u.Id,
                                       u.Name,
                                       u.Email,
                                       u.PasswordHash,
                                       u.PasswordSalt,
                                       u.IsConfirmed,
                                       Role = u.InternalRole,
                                       u.LastActivityAt,
                                       u.CreatedAt
                                    }).ToTable("User");
        }
    }

 public class SiteMap : EntityConfiguration<Site>
    {
        public SiteMap()
        {
            HasKey(s => s.Id);
            Property(s => s.Id).IsIdentity();
            Property(s => s.HasDonationPage);
            Property(s => s.IsActive);
            Property(s => s.IsAdminRestricted);
            Property(s => s.IsPrivate);
            Property(s => s.Slug).IsRequired().IsVariableLength().HasMaxLength(125);
            Property(s => s.CreatedAt);
            MapSingleType(s => new
                                   {
                                   s.Id,
                                   UserId = s.User.Id,
                                   ThemeId = s.Theme.Id,
                                   s.HasDonationPage,
                                   s.IsActive,
                                   s.IsAdminRestricted,
                                   s.IsPrivate,
                                   s.Slug,
                                   s.CreatedAt
                                   }).ToTable("Sites");
        }
    }

这是正确的还是应该以不同的方式建模?我曾经用以下方法手动映射关系:

Relationship(u => u.SitesFollowing).FromProperty(s => s.UsersFolling);

语法,但现在我已升级到CPT4,不再有效。我希望EF在数据库中创建一个名为SiteFolling_User的关系表,然后使用SiteId和UserId,但它创建了一个Site_User表,其中包含SiteId(PK)和UserId(FK),一个Follow_Site,其中包含Follow_Id(PK)和Site_Id(FK)以及一个Following_User具有Following_Id(PK)和User_Id(FK)的表。

任何指导都会很棒,谢谢!

1 个答案:

答案 0 :(得分:0)

如果您想映射many-to-many关系(用户拥有多个网站),您可以在UserMap旁边的CTP4中执行以下操作:

this.HasMany(u => u.Sites).WithMany();

您也可以描述多对多关系所在的表格:

this.HasMany(u => u.Sites).WithMany().Map(new StoreTableName("SiteFollowing_UserFollowing", "dbo"), (u, s) => new { UserId = u.Id, SiteId = s.Id });

如果这不是您要找的,请告诉我。干杯!