EF 7。包含在单元测试中失败

时间:2017-09-09 10:49:28

标签: linq unit-testing in-memory-database entity-framework-core

试图找出我的单元测试失败的原因我将我的项目从core1.1升级到了core 2,看起来.Include方法似乎有所改变。

我得到的错误是错误

  

从'VisitLambda'调用时,重写类型的节点   'System.Linq.Expressions.ParameterExpression'必须返回非null   相同类型的值。或者,覆盖'VisitLambda'和   将其更改为不访问此类儿童。

作为一种解决方案,我认为不能使用MOQ然后我不需要使用EF Core InMemory数据库,但我想知道我做错了什么。查询对我来说似乎很好 的 TEST

var options = new DbContextOptionsBuilder<ApplicationContext>()
                .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                .Options;

            using (var context = new ApplicationContext(options))
            {
                context.Add<User>(
                    new User { UsersId = 1, UsersEmail = "a@a.com", EmployeeId = 50, UserType = UserType.Employee });

                context.Add<Employee>(
                    new Employee
                    {
                        EmpFirstName = "FirstName1",
                        EmpLastName = "LastName1",
                        EmployeeId = 50,
                        CompanyId = 158
                    });

                context.Add<UserAgencyLink>(
                    new UserAgencyLink
                    {
                        UserAgencyLinkId = 1,
                        CompanyId = 158,
                    });
                context.Add<UserCompanyLink>(
                    new UserCompanyLink
                    {
                        UsersCompanyLinkId = 1,
                        CompanyId = 158,
                        CompanyFrequencyId = 80,
                    });
                context.Add<UserTheme>(
                    new UserTheme
                    {
                        UserThemeId = 1,
                        UserId = 1,
                        PrimaryColor = "teal",
                        NavbarColor = "Yellow",
                        NavbarType = true
                    });

                context.SaveChanges();

                var user = context.Set<User>()
                    .Include(_ => _.AgencyLinks)
                    .Include(_ => _.CompanyLinks)
                    .Include(_ => _.UserTheme)
                    .SingleOrDefaultAsync(_ => _.UsersEmail == "a@a.com");

            }

模型

public class User
    {

        [Key]
        [Column("pkUsersID")]
        public long UsersId { get; set; }

        [Column("fkUserType")]
        public UserType UserType { get; set; }

        [Column("fkCompanyGroupID")]
        public long? CompanyGroupId { get; set; }

        public virtual CompanyGroup CompanyGroup { get; set; }

        [Column("fkEmpID")]
        public long? EmployeeId { get; set; }

        public virtual Employee Employee { get; set; }

        [Column("fkAgencyID")]
        public long? AgencyId { get; set; }

        public virtual Agency Agency { get; set; }

        public virtual ICollection<UserAgencyLink> AgencyLinks { get; set; }

        public virtual ICollection<UserCompanyLink> CompanyLinks { get; set; }
}

public class UserAgencyLink
    {
        [Key]
        [Column("pkUsersAgencyLinkID")]
        public long UserAgencyLinkId { get; set; }

        [Column("fkUsersID")]
        public long UserId { get; set; }

        public virtual User User { get; set; }

        [Column("fkCompanyID")]
        public long CompanyId { get; set; }

        public virtual Company Company { get; set; }
    }
public class UserCompanyLink
    {
        [Key]
        [Column("pkUsersCompanyLinkID")]
        public long UsersCompanyLinkId { get; set; }

        [Column("fkUserID")]
        public long UserId { get; set; }

        public virtual User User { get; set; }

        [Column("fkCompanyID")]
        public long CompanyId { get; set; }

        public virtual Company Company { get; set; }

        [Column("fkCompanyFrequencyID")]
        public long CompanyFrequencyId { get; set; }

        public bool Checked { get; set; }
    }

0 个答案:

没有答案