EF 4.2 多对多错误?
我们正在使用 ASP.NET 成员资格来管理我们的用户,我在使用 Entity Framework 4.2 时发现了一个问题
,我在数据库中有一个名为 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; }
}
问题是:当我尝试在代理类中选择我的 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'.
我发现发生此错误是因为我的类名是单数:Aspnet_User 而不是 Aspnet_Users。
但是,如果我在配置中创建了关系(请参阅类 aspnet_UserConfiguration),系统不应尝试获取 aspnet_UsersAgent?为什么它尝试获取 dbo.ClassName1_ClassName2?这是一个错误吗?
先感谢您。
We are using the ASP.NET membership to manager our user and I found a problem using Entity Framework 4.2
I have the table in db called Aspnet_Users and I Created My class as:
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; }
}
My Entity Configuration:
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");
}
And Finally my Agent table:
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; }
}
The problem is: When I try to select my Aspnet_User in the class Agent, I'm getting a error:
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'.
I figured out that this error occur because my classname is in singular: Aspnet_User instead of Aspnet_Users.
But if I created the relationship inside my Configuration(see class aspnet_UserConfiguration), the system should not try to get aspnet_UsersAgent? Why is it try to get dbo.ClassName1_ClassName2? Is It a bug?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论