简体   繁体   中英

How to fix System.InvalidOperationException error?

I am trying to join two tables using Entity Framework but getting System.InvalidOperationException error. The error message is:

The property 'MasterCompany' is not a navigation property of entity type 'UnitOfMeasure'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.`.

I am not sure why it is throwing it.

Other Attempts:

  1. I also tried adding public virtual MasterCompany MasterCompany { get; set; } public virtual MasterCompany MasterCompany { get; set; } public virtual MasterCompany MasterCompany { get; set; } in UnitOfMeasure but that throws Invalid column error .
  2. Also added [Key] in MasterCompany table for MasterCompanyId .
  3. But both of these changes throws Invalid column name 'IsDeleted'. error.

DAL Models:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
  public DbSet<UnitOfMeasure> UnitOfMeasure { get; set; }
  public DbSet<MasterCompany> MasterCompany { get; set; }
}

public class UnitOfMeasure : PasBase, IAudit
{
    [Key]
    public long UnitOfMeasureId { get; set; }
    public string Description { get; set; }
    public string ShortName { get; set; }
    public string Memo { get; set; }
    public string Standard { get; set; }
    // [ForeignKey("MasterCompanyId")]
    public Int32 MasterCompanyId { get; set; }
    public bool IsActive { get; set; }
    public bool IsDeleted { get; set; }
    [NotMapped]
    public string UploadStatus { get; set; }
    [ForeignKey("MasterCompanyId")]
    public virtual MasterCompany MasterCompany { get; set; }
}

public class MasterCompany:AuditableEntity
{
    public int MasterCompanyId { get; set; }
    public string CompanyName { get; set; }
    public string TaxId { get; set; }
    public string EmailAddress { get; set; }
    public string Address { get; set; }
    public bool? IsActive { get; set; }
}

Repository:

private ApplicationDbContext _appContext => (ApplicationDbContext)_context;

public IEnumerable<DAL.Models.UnitOfMeasure> getUnitOfMeasureData()
{
    return _appContext.UnitOfMeasure
        .Include("MasterCompany")
        .Where(c => c.IsDeleted == false || c.IsDeleted == null)
        .OrderByDescending(c => c.UnitOfMeasureId)
        .ToList();
}

You need to add a MasterCompany property in the class UnitOfMeasure

public class UnitOfMeasure : PasBase, IAudit
{
    [Key]
    public long UnitOfMeasureId { get; set; }
    public string Description { get; set; }
    public string ShortName { get; set; }
    public string Memo { get; set; }
    public string Standard { get; set; }
    public Int32 MasterCompanyId { get; set; }
    [ForeignKey("MasterCompanyId")]
    public MasterCompany MasterCompany { get; set; }
    public bool IsActive { get; set; }
    public bool IsDeleted { get; set; }
    [NotMapped]
    public string UploadStatus { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM