使用数据库优先方法向Model添加新字段MVC3不会从数据库中提取数据

时间:2022-03-13 08:03:53

I'm trying to add a new field to an existing database (MS SQL Server) and MVC 3 model using the database-first approach. The database and model structure is very complex and the DB hold lots of data, so it is not an option to drop the database and rebuild it. So far I've tried the following to no avail:

我正在尝试使用数据库优先方法向现有数据库(MS SQL Server)和MVC 3模型添加新字段。数据库和模型结构非常复杂,数据库包含大量数据,因此不能删除数据库并重建数据库。到目前为止,我已经尝试了以下无济于事:

  1. Added the field into the database first and then went into the .edmx file. I clicked the table and selected "Update Model from Database"

    首先将字段添加到数据库中,然后进入.edmx文件。我点击了表格并选择了“从数据库更新模型”

    • This adds the field into the model, but the data is not pulled from the database. I checked this by breaking during debugging and checking the model list. It just gives the field the default value from the model.

    • 这会将字段添加到模型中,但不会从数据库中提取数据。我通过在调试和检查模型列表期间断开来检查这一点。它只是为该字段提供模型的默认值。

  2. Added the field in the model first and then added the field to the database (with script below).

    首先在模型中添加字段,然后将字段添加到数据库(使用下面的脚本)。

    • This creates a second model variable with "_1" and maps it to the database field.
    • 这将创建一个带有“_1”的第二个模型变量,并将其映射到数据库字段。

Code Reference

The script for adding the field into the database table:

用于将字段添加到数据库表中的脚本:

ALTER TABLE stores
ADD active bit NOT NULL DEFAULT 1

ALTER TABLE存储ADD有效位NOT NULL DEFAULT 1

The code for the model that is added in:

添加的模型代码:

public bool active { get; set; }

public bool active {get;组; }

The code for populating the list of stores:

填充商店列表的代码:

var storeList = (from n in db.stores select n).ToList();

var storeList =(从db.stores中的n选择n).ToList();

How do I get it to work where the model field will be populated by the data from the database and not by the model default value?

如何使用数据库中的数据填充模型字段而不是模型默认值?

1 个解决方案

#1


0  

In a situation like this, you may need to delete and subsequently recreate your Entity Model using the wizard.

在这种情况下,您可能需要使用向导删除并随后重新创建实体模型。

#1


0  

In a situation like this, you may need to delete and subsequently recreate your Entity Model using the wizard.

在这种情况下,您可能需要使用向导删除并随后重新创建实体模型。