EF 4.2很多很多bug?

时间:2011-12-22 10:48:21

标签: c# entity-framework many-to-many

我们使用ASP.NET成员资格来管理我们的用户,并且我发现使用Entity Framework 4.2存在问题

我在db中有一个名为Aspnet_Users的表,我创建了我的类:

public class Aspnet_User
{
    [Key]
    public Guid ID { get; set; }
    public Guid ApplicationID { get; set; }
    public string UserName { get; set; }
    public string LoweredUserName { get; set; }
    public string MobileAlias { get; set; }
    public bool IsAnonymous { get; set; }
    public DateTime LastActivityDate { get; set; }

    public virtual Aspnet_Membership Aspnet_Membership { get; set; }
    public virtual Aspnet_Profile Aspnet_Profile { get; set; }

    public virtual ICollection<Aspnet_Role> Aspnet_Roles { get; set; }
    public virtual ICollection<Agent> Agents { get; set; }
}

我的实体配置:

public class Aspnet_UserConfiguration : EntityTypeConfiguration<Aspnet_User>
{
    public Aspnet_UserConfiguration()
        : base()
    {
        HasKey(p => p.ID);

        Property(p => p.ID).HasColumnName("UserId").IsRequired();

        Property(p => p.MobileAlias).IsOptional();

        HasOptional(p => p.Aspnet_Profile).WithRequired();
        HasOptional(p => p.Aspnet_Membership).WithRequired();

        HasMany(p => p.Aspnet_Roles).WithMany(a => a.Aspnet_Users).Map(mc =>
            {
                mc.MapLeftKey("UserId");
                mc.MapRightKey("RoleId");
                ToTable("aspnet_UsersInRoles");
            });

        HasMany(p => p.Agents).WithMany(a => a.Aspnet_Users).Map(mc =>
            {
                mc.MapLeftKey("UserId");
                mc.MapRightKey("AgentId");
                ToTable("aspnet_UsersAgent");
            });

        ToTable("aspnet_Users");
    }

最后是我的代理人表:

public class Agent
{
    [Key]
    public int ID { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string ShortBio { get; set; }
    public string ExtendedBio { get; set; }
    public string Tel { get; set; }
    public string Fax { get; set; }
    public string Email { get; set; }
    public string CompanyName { get; set; }
    public string ImageURL { get; set; }
    public string BranchName { get; set; }
    public string UserPassword { get; set; }
    public DateTime? LastLogin { get; set; }
    public bool? Active { get; set; }
    public bool ShowMap { get; set; }
    public string BatchNoPrefix { get; set; }

    public string FullName
    {
        get { return string.Format("{0} {1}", FirstName, Surname); }
    }

    public virtual AgentsAddress Address { get; set; }

    public virtual ICollection<PaymentNotification> PaymentNotifications { get; set; }
    public virtual ICollection<Authority> Authorities { get; set; }
    public virtual ICollection<Aspnet_Users> Aspnet_Users { get; set; }
}

问题是:当我尝试在类Agent中选择我的Aspnet_User时,我收到一个错误:

Invalid object name 'dbo.Aspnet_UserAgent'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Aspnet_UserAgent'.

我发现发生此错误是因为我的classname是单数:Aspnet_User而不是Aspnet_Users。

但是如果我在Configuration中创建了关系(请参阅类aspnet_UserConfiguration),系统不应该尝试获取aspnet_UsersAgent?为什么要尝试获取dbo.ClassName1_ClassName2?这是一个错误吗?

提前谢谢。

0 个答案:

没有答案