简体   繁体   中英

Unable to access model properties of database context - .net core (3.1)

I'm new to .net core and currently stuck in very odd and unfamiliar situation. Let me explain you my problem in steps:

Step 1 First I created basic models

    [Table("ApplicationUserRights", Schema = "Security")]
    public class ApplicationUserRight
    {
        public Guid RoleId { get; set; }
        public int MenuId { get; set; }
        public int ClaimId { get; set; }

        [ForeignKey("RoleId")]
        public virtual ICollection<ApplicationRole> ApplicationRoles { get; set; }

        [ForeignKey("MenuId")]
        public virtual ICollection<ApplicationMenu> ApplicationMenus { get; set; }

        [ForeignKey("ClaimId")]
        public virtual ICollection<ApplicationClaim> ApplicationClaims { get; set; }
    }

    [Table("ApplicationClaim", Schema = "Security")]
    public class ApplicationClaim
    {
        [Required]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [MaxLength(100)]
        public string Claim { get; set; }
        public int EnumValue { get; set; }
    }


Step 2
Then I created my database context something like this.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, 
        ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
    {
      public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        ....

        public DbSet<ApplicationClaim> ApplicationClaims { get; set; }
        public DbSet<ApplicationUserRight> ApplicationUserRights { get; set; }

        ....
    }

Step 3:
Registered that database context in startup.cs.

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

services.AddDefaultIdentity<ApplicationUser>();

Step: 4
Scoped a class on which database context will work.

...
services.AddScoped<IMiddlewareAuthorization, MiddlewareAuthorization>();
...

Step: 5 Finally when I try to make a linq query, properties just won't show up

public class MiddlewareAuthorization : IMiddlewareAuthorization
    {
        private readonly ApplicationDbContext db;
        public MiddlewareAuthorization(ApplicationDbContext context) 
        {
            db = context;
        }
        public IList<string> GetAuthorizedURLs()
        {
            var Urls = (from abc in db.ApplicationUserRights
                        join xyz in db.ApplicationClaims on abc.??? equals xyz.???);

            return new List<string>();
        }
    }

That is very frustrating to me cause I just cannot figure the actual cause of the problem.

Finally after wasting all day just noticed that I forgot to import System.Linq .
It works fine now.

Everything looks fine! Are you sure to import all the using statements?

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