正如我在主题标题中所述,DataContext.SubmitChanges()方法存在问题。我有两个表:用户和管理员(以及其他角色,例如Doctors等,但现在我正在使用这两个表)。 这两个表的SQL脚本: 用户:
`CREATE TABLE [dbo].[Users](
[usrId] [int] IDENTITY(1,1) NOT NULL,
[login] [nvarchar](50) NOT NULL,
[password] [nvarchar](50) NOT NULL,
[role] [char](3) NOT NULL,
[staffId] [int] NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[usrId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]`
对于管理员:
`CREATE TABLE [dbo].[Administrators](
[adminId] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NOT NULL,
[surname] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Administrators] PRIMARY KEY CLUSTERED
(
[adminId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]`
现在,管理员在Administrators.adminId = Users.staffId上加入用户(adminId为PK,staffId为FK)。简单-我知道当User.staffId == Administrator.adminId AND User.role =“ ADM”时,表Users中的User是管理员。
那么探针在哪里? 我为此数据库进行了LINQ映射:
[Database(Name = "BD_PROJ_GKiO3")]
public class BD_PROJ_GKiO3 : DataContext
{
public Table<User> Users;
public Table<Doctor> Doctors;
public Table<LaboratoryWorker> LaboratoryWorkers;
public Table<LaboratoryManager> LaboratoryManagers;
public Table<Registrator> Registrators;
public Table<Administrator> Administrators;
public Table<Appointment> Appointments;
public Table<ExaminationType> ExaminationTypes;
public Table<LaboratoryExamination> LaboratoryExaminations;
public Table<Patient> Patients;
public Table<PhysicalExamination> PhysicalExaminations;
public BD_PROJ_GKiO3(IDbConnection connection) : base(connection)
{
}
}
以及用户和管理员的映射:
[Table(Name = "Users")]
public class User
{
//kolumna PK w tabeli Users
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "Int NOT NULL IDENTITY")]
public int usrId { get; set; }
//Reszta kolumn
[Column] public string login { get; set; }
[Column] public string password { get; set; }
[Column] public string role { get; set; }
[Column] public string status { get; set; }
[Column] public int staffId { get; set; }
//Mapowanie relacji z tabelami aktorów
private EntitySet<Doctor> _Doctors;
[Association(Storage = "_Doctors", OtherKey = "docId", ThisKey = "staffId")]
public EntitySet<Doctor> Doctors
{
get { return this._Doctors; }
set { this._Doctors.Assign(value); }
}
private EntitySet<Registrator> _Registrators;
[Association(Storage = "_Registrators", OtherKey = "registratorId", ThisKey = "staffId")]
public EntitySet<Registrator> Registrators
{
get { return this._Registrators; }
set { this._Registrators.Assign(value); }
}
private EntitySet<Administrator> _Administrators;
[Association(Storage = "_Administrators", OtherKey = "adminId", ThisKey = "staffId", IsForeignKey = true)]
public EntitySet<Administrator> Administrators
{
get { return this._Administrators; }
set { this._Administrators.Assign(value); }
}
private EntitySet<LaboratoryManager> _LaboratoryManagers;
[Association(Storage = "_LaboratoryManagers", OtherKey = "laboratoryPrincipalId", ThisKey = "staffId")]
public EntitySet<LaboratoryManager> LaboratoryManagers
{
get { return this._LaboratoryManagers; }
set { this._LaboratoryManagers.Assign(value); }
}
private EntitySet<LaboratoryWorker> _LaboratoryWorkers;
[Association(Storage = "_LaboratoryWorkers", OtherKey = "laboratoryWorkerId", ThisKey = "staffId")]
public EntitySet<LaboratoryWorker> LaboratoryWorkers
{
get { return this._LaboratoryWorkers; }
set { this._LaboratoryWorkers.Assign(value); }
}}
[Table(Name = "Administrators")]
public class Administrator
{
//PK w tabeli Administrators
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int adminId { get; set; }
//Reszta kolumn
[Column] public string name { get; set; }
[Column] public string surname { get; set; }
//Mapowanie relacji tabeli Administrators z tabelą Users
private EntitySet<User> _Users;
[Association(Storage = "_Users", OtherKey = "staffId", IsForeignKey = true, ThisKey = "adminId")]
public EntitySet<User> Users
{
get { return this._Users; }
set { this._Users.Assign(value); }
}}
当然,这两个类都具有构造函数,但是我没有在这里粘贴它(当我必须这样做时,我会这样做)。
因此,在上面您可以看到我如何将表和关系从数据库映射到对象。
问题是当我尝试用表Administrators中的相关行更新表Users中的现有行时。我已经创建了用户实例(toUpdate),用于存储数据,并使用该表的行进行更新,接下来,我向其中添加一个管理员实例数据(admin)。首先-我正在获取DataContext对象,其次-我正在数据库中寻找供用户编辑的对象。
首先:
BD_PROJ_GKiO3 newDB = new BD_PROJ_GKiO3(Program.getConnection());
User u = newDB.Users.SingleOrDefault(us => us.usrId == currentlyEdited.usrId);
找到了正确的用户(u),所以我正在尝试执行以下代码:
u.login = toUpdate.login;
u.role = toUpdate.role;
u.status = toUpdate.status;
u.staffId = toUpdate.staffId;
u.Administrators.ElementAt(0).name = admin.name;
u.Administrators.ElementAt(0).surname = admin.surname;
newDB.SubmitChanges();
在执行最后一行(newDB.SubmitChanges())期间,抛出NullReferenceException。
堆栈跟踪:ExceptionStackTrace
在此先感谢所有尝试帮助我的人,我将非常感谢! 编辑:我只是忘了提一下,我认为以这种方式创建和映射的数据库可能很好用,因为当我的应用程序启动时,我正在创建新的数据库作为程序的公共字段(应用程序根目录)并使用它,用户有可能登录,然后才能正常运行(登录时,我们必须检查用户和管理员的数据)。