InvalidOperationException:属性'CMemberId'不是实体类型'Councillors'的导航属性

时间:2019-07-14 22:16:59

标签: c# xunit

我是C#和.Net的新手,并且我有一个C#应用程序,该应用程序使用开放数据从mysql数据库中显示有关市议会的信息。问题是我尝试按照要求我创建一个接口并设法通过测试的教程使用XUnit和模拟创建单元测试,但是当我尝试运行该应用程序时,它给我以下信息:“发生了未处理的异常在处理请求时。” InvalidOperationException:属性'CMemberId'不是实体类型'Councillors'的导航属性。 “ include(string)”方法只能与“。”一起使用。导航属性名称的分隔列表。

我试图运行该应用程序,它给我以下信息:“处理请求时发生未处理的异常”。 InvalidOperationException:属性'CMemberId'不是实体类型'Councillors'的导航属性。 “ include(string)”方法只能与“。”一起使用。导航属性名称的单独列表。我曾尝试查看导航属性,但无法理解我的问题。

界面

public interface ICouncillorsRepository
    {
        Task<List<Councillors>> GetAll();
        Task<Councillors> GetOne(int id);
        Task SaveChanges();
    }

测试

 public class CouncillorsControllerTests
    {
        private Mock<ICouncillorsRepository> councillorsRepoMock;
        private CouncillorsController controller;

        public CouncillorsControllerTests()
        {
            councillorsRepoMock = new Mock<ICouncillorsRepository>();
            controller = new CouncillorsController(councillorsRepoMock.Object);
        }
        [Fact]
        public async Task IndexTest_ReturnsViewWithCouncillorsList()
        {
            // Arrange
            var mockCouncillorsList = new List<Councillors>
            {
                new Councillors { CMemberFname = "mock Councillor 1" },
                new Councillors { CMemberFname = "mock Councillor 2" }
            };
            councillorsRepoMock.Setup(repo => repo.GetAll()).Returns(Task.FromResult(mockCouncillorsList));

            // Act
            var result = await controller.Index(); 

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            var model = Assert.IsAssignableFrom<IEnumerable<Councillors>>(viewResult.ViewData.Model);
            Assert.Equal(2, model.Count());

}

我的控制器

public class CouncillorsController : Controller
    {
        private readonly ICouncillorsRepository _councillorsRepository;

        public CouncillorsController(ICouncillorsRepository councillorsRepository)
        {
            _councillorsRepository = councillorsRepository;
        }

        // GET: Councillors
        public async Task<IActionResult> Index()
        {
            return View(await _councillorsRepository.GetAll());
        }

        // GET: Councillors/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var councillors = await _councillorsRepository.GetOne(id.Value);
            if (councillors == null)
            {
                return NotFound();
            }

            return View(councillors);
        }

    }
}

我的DbContext

 public class sample6Context : DbContext
    {
        public sample6Context()
        {
        }

        public sample6Context(DbContextOptions<sample6Context> options)
            : base(options)
        {
        }

        public virtual DbSet<Councillors> Councillors { get; set; }
        public virtual DbSet<Data> Data { get; set; }
        public virtual DbSet<Postcode> Postcode { get; set; }
        public virtual DbSet<Wards> Wards { get; set; }
}

回购

 public class CouncillorsRepository : ICouncillorsRepository
    {
        private readonly sample6Context _context;
        public CouncillorsRepository(sample6Context context)
        {
            _context = context;
        }
        public Task<List<Councillors>> GetAll() =>
            _context.Councillors.Include(p => p.CWard).AsNoTracking().OrderByDescending(a => a.CWard).ToListAsync();

        public Task<Councillors> GetOne(int id) =>
            _context.Councillors.Include(a => a.CMemberFname).SingleOrDefaultAsync(m => m.CMemberId == id);
        public Task SaveChanges() =>
           _context.SaveChangesAsync();


    }
`````

the application is supposed to display information from the database(sample6Context) but after after creating the interface and the repo, i had to modify the controller to use the interface for mocking and i get the "An unhandled exception occurred while processing the request" error which further indicated "InvalidOperationException: The property 'CMemberId' is not a navigation property of entity type 'Councillors'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names" and points to the "Controllers.CouncillorsController.Index() in CouncillorsController.cs

            return View(await _councillorsRepository.GetAll());

can someone please help me out here

参议员模型


    public  class Councillors
    {
        [Display(Name = "Member Id")]
        public int CMemberId { get; set; }
        [Display(Name = "First Name")]
        public string CMemberFname { get; set; }
        [Display(Name = "Last Name")]
        public string CMemberLname { get; set; }
        [Display(Name = "Political Party")]
        public string PoliticalParty { get; set; }
        [Display(Name = "Ward Name")]
        public string WardName { get; set; }
        public string CWardId { get; set; }
        public string Url { get; set; }
        [ForeignKey("Wards")]
        public virtual Wards CWard { get; set; }
    }

沃兹模型

public  class Wards
    {
        public Wards()
        {
            Councillors = new HashSet<Councillors>();
            Data = new HashSet<Data>();
        }


        public string WardId { get; set; }
        public string WardName { get; set; }
        public string NumberOfCouncillors { get; set; }
        public string TotalPopulation { get; set; }
        public string MalePopulation { get; set; }
        public string FemalePopulation { get; set; }
        public string _015Years { get; set; }
        public string _1624Years { get; set; }
        public string _2544Years { get; set; }
        public string _4564Years { get; set; }
        public string _6574Years { get; set; }
        public string _75Years { get; set; }
        public string TotalHouseholds { get; set; }
        public string OnePersonHouseholdAgedOver65 { get; set; }
        public string OnePersonHouseholdAgedUnder65 { get; set; }
        public string MarriedCoupleWithNoChildren { get; set; }
        public string MarriedCoupleWithDependentChildren { get; set; }
        public string MarriedCoupleWithNonDependentChildren { get; set; }
        public string CohabitingCoupleWithNoChildren { get; set; }
        public string CohabitingCoupleWithDependentChildren { get; set; }
        public string CohabitingCoupleWithNonDependentChildren { get; set; }
        public string LoneParentWithDependentChildren { get; set; }
        public string LoneParentWithNonDependentChildren { get; set; }
        public string TotalDwellings { get; set; }
        public string HouseOrBungalow { get; set; }
        public string FlatOrApartment { get; set; }
        public string OtherTemporaryStructure { get; set; }
        public string TotalOwnedOrMortgagedDwellings { get; set; }
        public string TotalSocialRentedDwellings { get; set; }
        public string TotalPrivAteRentedDwellings { get; set; }
        public string TotalLivingRentFree { get; set; }
        public string CtBandA { get; set; }
        public string CtBandB { get; set; }
        public string CtBandC { get; set; }
        public string CtBandD { get; set; }
        public string CtBandE { get; set; }
        public string CtBandF { get; set; }
        public string CtBandG { get; set; }
        public string CtBandH { get; set; }
        public string WorkingAgePartTimeEmployed { get; set; }
        public string WorkingAgeFullTimeEmployed { get; set; }
        public string WorkingAgeSelfEmployedPartTimeWithEmployees { get; set; }
        public string WorkingAgeSelfEmployedFullTimeWithEmployees { get; set; }
        public string WorkingAgeSelfEmployedPartTimeWithoutEmployees { get; set; }
        public string WorkingAgeSelfEmployedFullTimeWithoutEmployees { get; set; }
        public string WorkingAgeUnemployed { get; set; }
        public string FullTimeStudents { get; set; }
        public string ClaimantCount { get; set; }
        public string BenefitClaimantsAsAProportionOfResidentsAged1664 { get; set; }
        public string PercentageOfChildrenInPovertyBeforeHousingCosts { get; set; }
        public string PercentageOfChildrenInPovertyAfterHousingCosts { get; set; }
        public string ManagersDirectorsAndSeniorOfficials { get; set; }
        public string Professionals { get; set; }
        public string AdminAndSecretarial { get; set; }
        public string SkilledTrades { get; set; }
        public string SalesAndCustomerService { get; set; }
        public string ProcessPlantAndMachinePeratives { get; set; }
        public string ElementaryOccupations { get; set; }
        public string MeanIncome { get; set; }
        public string MedianIncome { get; set; }
        public string ModalIncome { get; set; }
        public string LowerQuartile { get; set; }

        public virtual ICollection<Councillors> Councillors { get; set; }
        public virtual ICollection<Data> Data { get; set; }
    }

0 个答案:

没有答案