简体   繁体   English

模型不更新数据库

[英]Model doesn't update the database

For some reason the method dbase.SaveChanges() can't update the database because roomid should be null. 由于某种原因,方法dbase.SaveChanges()无法更新数据库,因为roomid应该为null。 But the debugger does say that room1.id is set to 2? 但是调试器确实说room1.id设置为2?

Can somebody please help me with this ? 有人可以帮我吗?

private void button1_Click(object sender, EventArgs e)
{
    Room room1 = new Room();
    room1.Id = 2;
    dbase.AddToRoom(room1);
    dbase.SaveChanges();
}

i'm not sure this is actually ADO.NET, it seems more like entity framework or linq to SQL. 我不确定这实际上是ADO.NET,似乎更像是实体框架或SQL的linq。

It's also not quite clear what you're trying to do. 还不清楚您要做什么。 If you are trying to add a new Room to the database, i would advise setting some properties, otherwise you are just inserting an empty room. 如果您要向数据库添加一个新的房间,我建议您设置一些属性,否则您将插入一个空房间。

var room1 = new Room();
room1.RoomNumber = 4;
room1.Available = true;
//...
dbase.AddToRoom(room1);
dbase.SaveChanges();

If this is what you are trying to do, then you do not need to specify the ID for the room (if the ID column is set to auto-increment that is.) 如果这是您要执行的操作,则无需指定房间的ID(如果ID列设置为“自动递增”)。

if you are trying to update a room instead of inserting a new one you can do something like this: 如果您要更新房间而不是插入新房间,则可以执行以下操作:

var roomToUpdate = dbase.Rooms.Where(x => x.id == id).FirstOrDefault();
roomToUpdate.Property ="something";
//...
dbase.Update(roomToUpdate)
dbase.SaveChanges();

Again, its not entirely clear what exactly you are trying to do so if i'm wrong, please elaborate a little more. 同样,如果我错了,还不清楚您到底要做什么,请详细说明一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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