2010-08-01 84 views
0

我正在使用的网站具有用户和网站的概念。用户可以加入全球网站并在(www.globalsite.com/minisite)内创建小型网站。用户也可以关注由其他用户创建的其他迷你网站。想想看,如果Twitter允许你加入一组凭证并创建不同的帐户,一个用于@personal use,@business use等。域模型问题

我使用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在DB中使用SiteId和UserId在名为SiteFolling_UserFollowing的数据库中创建一个关系表,但它使用SiteId(PK)和UserId(FK),带有Following_Id(PK)和Site_Id(FK)的Following_Site和Following_User创建了Site_User表表与Following_Id(PK)和User_Id(FK)。

任何指导将是伟大的,谢谢!

+0

你可以发布你当前的映射吗? – TheCloudlessSky 2010-08-02 00:06:17

+0

@TheCloudlessSky - 我添加了映射代码,有什么想法? – Paul 2010-08-02 14:53:14

回答

0

如果你想映射many-to-many关系(用户有很多网站),你可以做以下的CTP4在侧UserMap

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 }); 

如果这不是你要找的,请让我知道。干杯!