为什么没有返回添加到SQL Server表的行的详细信息?

时间:2017-06-12 12:54:20

标签: c# sql-server entity-framework entity-framework-core

我正在使用Microsoft.EntityFrameworkCore version =“1.1.2”和存储过程,其中主键由SQL DBMS自动生成

我认为下面的代码会返回我添加的行的详细信息,但事实并非如此。

我的代码是......

            using (DataBaseContext dbInstance = new DataBaseContext())
            {
                Table newTable = new Table()
                {
                    tableName = addTable_TableNameTextBox.Text,
                    tableDescription = addTable_TableDescriptionTextBox.Text,
                    tableLongDescription = addTable_TableLongDescriptionTextBox.Text
                };
                DbInstance.DbTables.Add(newTable);
                DbInstance.SaveChanges();
                DbInstance.Entry(newTable).GetDatabaseValues();
                // The rows are added by the instructions above but GetDatabaseValues() doesn't retrieve the data that was added (I thought it would).
                // The row is being added OK and I can retrieve it using the following instruction (when I explicitly specify the key). 
                NewTable = dbInstance.DbTables.Find(97);
            }

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要刷新上下文,之后您的newTable变量将包含您的详细信息(我假设该表的其他属性)。

using (DataBaseContext dbInstance = new DataBaseContext())
{
    Table newTable = new Table()
    {
        tableName = addTable_TableNameTextBox.Text,
        tableDescription = addTable_TableDescriptionTextBox.Text,
        tableLongDescription = addTable_TableLongDescriptionTextBox.Text
    };
    DbInstance.DbTables.Add(newTable);
    DbInstance.SaveChanges();

    // Refresh the context
    DbInstance.Entry(newTable).Reload();       
}