I need a view-model in my ASP.NET MVC 5 project, but when I added one to the models folder, a new entity was added to the database and I was forced to add a migration and update my database. I do not want this to happen, as it is a view-model I am adding and not a model I need to persist back to the database. I want to scaffold some of the controller and views so I have added a primary key to the class. I did not add the newly created view-model to my DbContext
class.
我在ASP.NET MVC 5项目中需要一个视图模型,但当我在模型文件夹中添加一个时,一个新实体被添加到数据库中,我*添加一个迁移并更新我的数据库。我不希望这种情况发生,因为它是我正在添加的视图模型,而不是我需要持久化回数据库的模型。我想构建一些控制器和视图,所以我在类中添加了一个主键。我没有将新创建的视图模型添加到我的DbContext类中。
ViewModel:
public class RolesViewModel
{
public int RolesViewModelId { get; set; }
public string Role { get; set; }
}
Is there a way to create a view-model that doesn't automatically get added to the DbContext
class, and therefore cause the data model to change?
有没有办法创建一个不会自动添加到DbContext类的视图模型,从而导致数据模型发生变化?
Many thanks,
Jason.
2 个解决方案
#1
0
In your case Scaffolding will add the following code below in your appContext Class
在您的情况下,Scaffolding将在您的appContext类中添加以下代码
public DbSet<RolesViewModel> RolesViewModel { get; set; }
public DbSet
You can still use scaffolding if you wish, however remember to remove that entry and no table will be created by code first. It will keep your database clean.
如果您愿意,您仍然可以使用脚手架,但请记住删除该条目,并且代码不会首先创建任何表。它将保持您的数据库清洁。
#2
7
Whether you call it a view model, an entity, etc. it's just semantics. Everything is just a class, and the context it's used in determines what you refer to it as. In the case of entities, that's adding a reference either explicitly or implicitly in your DbContext
, and that's the only way you'll end up with something added to your database. I emphasized the "or implicitly* part because if any class that is referenced in the your DbContext
, or any class connected to any class referenced there, also references your "view model", it will end up in your database. Entity Framework will automatically follow your class hierarchies and create tables for all relationships, even if you do not reference a particular class in those hierarchies directly in your DbContext
.
无论你称之为视图模型,实体等,它只是语义。一切都只是一个类,它所使用的上下文决定了你所指的是什么。对于实体,它会在您的DbContext中显式或隐式添加引用,这是您最终添加到数据库中的内容的唯一方法。我强调了“或者隐式*部分,因为如果你的DbContext中引用的任何类,或任何连接到那里引用的类的类,也引用了你的”视图模型“,它将最终出现在你的数据库中。实体框架将自动遵循您的类层次结构并为所有关系创建表,即使您没有直接在DbContext中引用这些层次结构中的特定类。
#1
0
In your case Scaffolding will add the following code below in your appContext Class
在您的情况下,Scaffolding将在您的appContext类中添加以下代码
public DbSet<RolesViewModel> RolesViewModel { get; set; }
public DbSet
You can still use scaffolding if you wish, however remember to remove that entry and no table will be created by code first. It will keep your database clean.
如果您愿意,您仍然可以使用脚手架,但请记住删除该条目,并且代码不会首先创建任何表。它将保持您的数据库清洁。
#2
7
Whether you call it a view model, an entity, etc. it's just semantics. Everything is just a class, and the context it's used in determines what you refer to it as. In the case of entities, that's adding a reference either explicitly or implicitly in your DbContext
, and that's the only way you'll end up with something added to your database. I emphasized the "or implicitly* part because if any class that is referenced in the your DbContext
, or any class connected to any class referenced there, also references your "view model", it will end up in your database. Entity Framework will automatically follow your class hierarchies and create tables for all relationships, even if you do not reference a particular class in those hierarchies directly in your DbContext
.
无论你称之为视图模型,实体等,它只是语义。一切都只是一个类,它所使用的上下文决定了你所指的是什么。对于实体,它会在您的DbContext中显式或隐式添加引用,这是您最终添加到数据库中的内容的唯一方法。我强调了“或者隐式*部分,因为如果你的DbContext中引用的任何类,或任何连接到那里引用的类的类,也引用了你的”视图模型“,它将最终出现在你的数据库中。实体框架将自动遵循您的类层次结构并为所有关系创建表,即使您没有直接在DbContext中引用这些层次结构中的特定类。