在这个问题上再次撕裂我的头发......使用EF 5 Code First,在代码中构建模型 - 编译,所以它在语法上是正确的,但是当代码构建模型时我得到一个异常。这是实体类(我也有 vaidation 属性,但为了便于阅读,在这里删除了它们):
[Table("USERS")]
public class User : IPEntity
{
#region Constructor (needs to initialize list objects for related entities)
public User()
{
this.Profiles = new List<Profile>();
this.ProfileDivs = new List<ProfileDiv>();
this.ProfileDepts = new List<ProfileDept>();
}
#endregion
#region Entity properties and validation attributes
[Key]
public long UserId { get; set; }
public long PclientId { get; set; }
public string UserName { get; set; }
public string UserDescription { get; set; }
public long? EmpId { get; set; }
public string MustChangePassword { get; set; }
public long? FailedLogins { get; set; }
public DateTime? LastLogin { get; set; }
public long? SequenceNumber { get; set; }
public string AllDivs { get; set; }
public string AllDepts { get; set; }
public string UserRole { get; set; }
public DateTime? BeginSupport { get; set; }
public DateTime? EndSupport { get; set; }
public string OneTimeAccess { get; set; }
public long? ClonedFromUser { get; set; }
public string Email { get; set; }
public string ResetEmail { get; set; }
public DateTime? ResetTimeout { get; set; }
public long? ChallengeFailures { get; set; }
public string PermUserRole { get; set; }
public DateTime? PasswordChangedDate { get; set; }
public virtual ICollection<Profile> Profiles { get; set; }
public virtual ICollection<ProfileDiv> ProfileDivs { get; set; }
public virtual ICollection<ProfileDept> ProfileDepts { get; set; }
public virtual WorkSession WorkSession { get; set; }
}
模型构建器类是:
public class User_Map : EntityTypeConfiguration<User>
{
public User_Map()
{
this.ToTable("USERS");
this.HasKey(t => new { t.UserId });
this.Property(t => t.UserId)
.HasColumnName("USER_ID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
.IsRequired();
this.Property(t => t.UserName)
.HasColumnName("USERNAME")
.IsRequired()
.HasMaxLength(25);
this.Property(t => t.PclientId)
.HasColumnName("PCLIENT_ID");
this.Property(t => t.EmpId)
.HasColumnName("EMP_ID");
this.Property(t => t.MustChangePassword)
.HasColumnName("MUST_CHANGE_PASSWORD")
.HasMaxLength(1);
this.Property(t => t.UserDescription)
.HasColumnName("USER_DESCRIPTION")
.HasMaxLength(80);
this.Property(t => t.FailedLogins)
.HasColumnName("FAILED_LOGINS");
this.Property(t => t.LastLogin)
.HasColumnName("LAST_LOGIN");
this.Property(t => t.SequenceNumber)
.HasColumnName("SEQUENCE_NUMBER");
this.Property(t => t.AllDivs)
.HasColumnName("ALL_DIVS")
.HasMaxLength(1);
this.Property(t => t.AllDepts)
.HasColumnName("ALL_DEPTS")
.HasMaxLength(1);
this.Property(t => t.UserRole)
.HasColumnName("USER_ROLE")
.HasMaxLength(2);
this.Property(t => t.BeginSupport)
.HasColumnName("BEGIN_SUPPORT");
this.Property(t => t.EndSupport)
.HasColumnName("END_SUPPORT");
this.Property(t => t.OneTimeAccess)
.HasColumnName("ONE_TIME_ACCESS")
.HasMaxLength(1);
this.Property(t => t.ClonedFromUser)
.HasColumnName("CLONED_FROM_USER");
this.Property(t => t.Email)
.HasColumnName("EMAIL")
.HasMaxLength(60);
this.Property(t => t.ResetEmail)
.HasColumnName("RESET_EMAIL")
.HasMaxLength(60);
this.Property(t => t.ResetTimeout)
.HasColumnName("RESET_TIMEOUT");
this.Property(t => t.ChallengeFailures)
.HasColumnName("CHALLENGE_FAILURES");
this.Property(t => t.PermUserRole)
.HasColumnName("PERM_USER_ROLE")
.HasMaxLength(2);
this.Property(t => t.PasswordChangedDate)
.HasColumnName("PASSWORD_CHANGED_DATE");
this.HasOptional(t => t.WorkSession)
.WithRequired(t => t.User);
// TODO: This is syntactically correct but model blows up!
this.HasMany(t => t.Profiles)
.WithRequired(t => t.User)
.HasForeignKey(t => t.UserId);
}
}
当模型构建器类的构造函数执行时,我在上面的行(注释之后)抛出了以下异常:
The expression 't => t.User' is not a valid property expression.
The expression should represent a property: C#: 't => t.MyProperty'
VB.Net: 'Function(t) t.MyProperty'.
Profiles 实体非常简单:
[Table("PROFILE")]
public class Profile : IPEntity
{
[Key, Column(Order = 0)]
[ForeignKey("UserId")]
public long UserId { get; set; }
[Key, Column(Order = 1)]
public string FunctionalArea { get; set; }
public int RightsId { get; set; }
public User User;
}
我已经在这个问题上打了两三天,如果有人能发现我的错误,我将非常感激!
谢谢,彼得
更新:通过阅读这篇文章,我发现了一个愚蠢的错误,因为我将某些东西声明为字段而不是属性......但现在我得到了一个我不明白的不同异常:
The navigation property 'UserId' is not a declared property on type 'Profile'.
Verify that it has not been explicitly excluded from the model and that
it is a valid navigation property.
这让我感到困惑,因为 UserId 是 Profile 上的声明属性...?
第二次更新:
我理解异常,但(因为没有内部异常细节)无法确定它来自哪里。我将 User_map 类更改为包括:
this.HasMany(t => t.Profiles)
.WithRequired(t => t.User)
.HasForeignKey(t => t.UserId)
.WillCascadeOnDelete(false);
和 Profile_map 类包括:
this.HasRequired(t => t.User)
.WithMany()
.HasForeignKey(t => t.UserId)
.WillCascadeOnDelete(false);
因为我必须使用现有的数据库,所以我坚持使用属性“UserId”作为两个表中的外键(在 User 表中,它是 Profiles 表的外键,在 Profiles 表中,它是 User 表的外键。)我将 .WillCascadeOnDelete(false) 添加到两者中,以防出现某种循环问题。就像我说的那样,我无法确定哪个地图正在炸毁,对两个构造函数的调用均无异常通过,只有当上下文中 OnModelCreating 的覆盖退出时才会引发异常。这是覆盖:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var mbProfile = new Profile_map();
modelBuilder.Configurations.Add(mbProfile);
var mbUser = new User_Map();
modelBuilder.Configurations.Add(mbUser);
base.OnModelCreating(modelBuilder);
}
我知道我仍然是 EF 新手,但我找不到问题(好吧,反正还没有……)
再次感谢。