我有两个名为 User 和 UserSession 的表,这两个表之间有两种关系:
UserSession.User -> User.UserSession
UserSession.SecondaryUser -> User.UserSessions
UserSession 将始终有一个用户,但可能没有 SecondaryUser。
我正在尝试使用 Code First Fluent 映射这些关系,下面是我当前的映射和类定义:
用户会话映射:
public UserSessionMap()
{
// Primary Key
this.HasKey(t => t.UserId);
// Properties
this.Property(t => t.UserId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Table & Column Mappings
this.ToTable("UserSession");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.SecondaryUserId).HasColumnName("SecondaryUserId");
this.Property(t => t.RoleNameCompanyInfo).HasColumnName("RoleNameCompanyInfo");
this.Property(t => t.RecordCreate).HasColumnName("RecordCreate");
this.Property(t => t.RecordUpdate).HasColumnName("RecordUpdate");
// Relationships
this.HasRequired(t => t.User).WithRequiredPrincipal(p => p.UserSession);
this.HasOptional(t => t.SecondaryUser).WithMany(p => p.UserSessions).HasForeignKey(p=>p.SecondaryUserId);
}
用户地图:
public class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
...snip...
// Table & Column Mappings
this.ToTable("User");
this.Property(t => t.Id).HasColumnName("Id");
...snip...
}
}
用户会话实体:
public partial class UserSession
{
public int UserId { get; set; }
[ForeignKey("SecondaryUser")]
public int? SecondaryUserId { get; set; }
public string RoleNameCompanyInfo { get; set; }
public DateTime RecordCreate { get; set; }
public DateTime RecordUpdate { get; set; }
public virtual User User { get; set; }
public virtual User SecondaryUser { get; set; }
}
用户实体:
public partial class User
{
public User()
{
this.Carts = new List<Cart>();
this.DirectDispenses = new List<DirectDispense>();
this.Downloads = new List<Download>();
this.MatchQuestionSessions = new List<MatchQuestionSession>();
this.Orders = new List<Order>();
this.UserSessions = new List<UserSession>();
this.UserUserRoles = new List<UserUserRole>();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int CompanyId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string PasswordSalt { get; set; }
public string Role { get; set; }
public bool Active { get; set; }
public string ColibriUserId { get; set; }
public Nullable<int> DispenserId { get; set; }
public Nullable<System.Guid> PasswordResetCode { get; set; }
public Nullable<System.DateTime> PasswordResetSent { get; set; }
public System.DateTime RecordUpdate { get; set; }
public System.DateTime RecordCreate { get; set; }
public virtual ICollection<Cart> Carts { get; set; }
public virtual Company Company { get; set; }
public virtual ICollection<DirectDispense> DirectDispenses { get; set; }
public virtual ICollection<Download> Downloads { get; set; }
public virtual ICollection<MatchQuestionSession> MatchQuestionSessions { get; set; }
public virtual ICollection<Order> Orders { get; set; }
public virtual ICollection<UserSession> UserSessions { get; set; }
public virtual UserSession UserSession { get; set; }
public virtual ICollection<UserUserRole> UserUserRoles { get; set; }
}
我也尝试将 [ForeignKey("SecondaryUserId") 添加到 SecondaryUser 属性,但没有运气。
基本上,UserId 和 SecondaryUserId 属性都应该映射到 User 的 Id 属性。EF 生成的错误是:
System.Data.SqlClient.SqlException:列名“SecondaryUser_Id”无效。
我认为它正在做的是在 User 表上寻找一个名为 SecondaryUser_Id 的属性,这显然不存在。我如何告诉它查看 User 对象上的 Id 属性。我发现的所有文章都显示了如何将 PK 映射到未命名为 EF 期望的 FK,这似乎是相反的情况,我找不到任何示例。
在 SO 上似乎有很多类似的问题与同样的错误,但这些解决方案都没有对我有用,而且这种情况似乎有所不同(正如我所说,这似乎与此错误的最常见原因相反)。