不返回实体框架映射值

时间:2019-04-23 14:45:45

标签: c# linq entity-framework-6

问题:

我有一个使用数据库优先方法从EF创建的DbContext类。但是访问User详细信息时,我无法验证。

输出

该代码正在运行,但验证未成功。提供了DbContextValidation类:

AlumniConnectEntities.cs

    namespace Test
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        using System.Data.Entity.Core.Objects;
        using System.Linq;

        public partial class AlumniConnectEntities : DbContext
        {
            public AlumniConnectEntities()
                : base("name=AlumniConnectEntities")
            {
            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }

            public virtual DbSet<Alumnus> Alumni { get; set; }
            public virtual DbSet<Job> Jobs { get; set; }
            public virtual DbSet<Role> Roles { get; set; }
            public virtual DbSet<Student> Students { get; set; }
            public virtual DbSet<sysdiagram> sysdiagrams { get; set; }
            public virtual DbSet<User> Users { get; set; }
        }
    }

Validation.cs

     public class StudentValidation
        {
                AlumniConnectEntities dbConnection = new AlumniConnectEntities();

                public bool CheckLogin()
                {
                    //bool loginSuccess = dbConnection.Users.Where(x => x.username == "rupee@gmail.com" && x.password == "rupee").Any();


                  var usersList = dbConnection.Users.ToList(); 

                 var loginSuccess= usersList.Where(x => x.username ==  
                 "rupee@gmail.com" && x.password == "rupee").Any(); 
                    return loginSuccess;
                }
            }

Users.cs

 public class Users
    {
        public int id { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public int Role_Id { get; set; }

        public virtual Roles Role { get; set; }
    }

Role.cs

public class Roles
    {
        public Roles()
        {
            this.Users = new HashSet<Users>();
        }

        public int id { get; set; }
        public string role_name { get; set; }

        public virtual ICollection<Users> Users { get; set; }
    }

表架构:

CREATE TABLE [dbo].[Role](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [role_name] [varchar](50) NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Users]    Script Date: 04/23/2019 20:50:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Users](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [username] [varchar](50) NULL,
    [password] [varchar](50) NULL,
    [Role_Id] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  ForeignKey [FK__Users__Role_Id__44FF419A]    Script Date: 04/23/2019 20:50:55 ******/
ALTER TABLE [dbo].[Users]  WITH CHECK ADD FOREIGN KEY([Role_Id])
REFERENCES [dbo].[Role] ([id])
GO

Getting the query when initiating the DBCOntex Class and not getting the property

EF Table Schema

0 个答案:

没有答案