0
我目前遇到一個問題,實體框架5沒有返回一個記錄,當該實體從具有我查詢的屬性的實體派生時存在。EF 5繼承查詢問題
這是我的實體:
public class Person
{
[Key]
public int Id { get; set; }
public string Email { get; set; }
}
public class User : Person
{
public DateTime LastLoginDate { get; set; }
}
我的數據上下文:
public class DataContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().Map(m => { m.ToTable("User"); });
modelBuilder.Entity<Person>()
.Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
庫:
public class UserRepository
{
private readonly DataContext _dataContext;
public UserRepository(DataContext dataContext)
{
_dataContext = dataContext;
}
public User GetByEmailAddress(string emailAddress)
{
if (string.IsNullOrEmpty(emailAddress))
throw new ArgumentException("The emailAddress parameter cannot be null or empty.", "emailAddress");
//This query works
var working = _dataContext.Persons
.FirstOrDefault(x => x.Email.Equals(emailAddress, StringComparison.CurrentCultureIgnoreCase));
//Where as this returns null
var query = _dataContext.Users
.FirstOrDefault(x => x.Email.Equals(emailAddress, StringComparison.CurrentCultureIgnoreCase));
return query;
}
}
如果我試圖通過電子郵件地址我用來查詢人獲得我正在尋找的記錄;但是,如果我通過電子郵件地址查詢用戶,我得到一個空結果。
任何建議將不勝感激。