Проблема:
У меня есть класс DbContext
, созданный из EF с использованием подхода на основе базы данных.Но при доступе к деталям User
я не могу проверить.
Вывод
Код работает, но проверка не удалась.Предоставляются классы DbContext
и Validation
:
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
Получение запроса при инициализации класса DBCOntex и отсутствие получения свойства