[英]Map model to existing database table using Entity Framework
I face some difficulties when attempt to map my class to existing table using Entity Framework. 尝试使用Entity Framework将我的类映射到现有表时遇到一些困难。 I currently using Code first but a table was created without it and i cannot map it to a model a created.
我目前首先使用Code,但是没有它就创建了一个表,我无法将它映射到创建的模型。
My class: 我的课:
[Table("AgentCallsByPartner")]
public class AgentCallsByPartnerModel
{
[Key]
public int AgentCallID { get; set; }
[Required]
public DateTime CallDate { get; set; }
public string CustomerID { get; set; }
public string AgentNo { get; set; }
public string AccountCreated { get; set; }
public string QuoteStarted { get; set; }
[Required]
public string GroupName { get; set; }
[Required]
public string GroupRollup { get; set; }
}
Context 上下文
public DbSet<AgentCallsByPartnerModel> AgentCallsByPartner { get; set; }
And this is my table 这是我的桌子
But when I try to get users from the database, I get an exception 但是当我尝试从数据库中获取用户时,我得到了一个例外
DbContext has changed since the database was created.... 自创建数据库以来,DbContext已更改....
So what is my mistake? 那我的错是什么? Thanks
谢谢
you can be perform several way i just describe few way 你可以通过几种方式进行几种方式
From the Tools menu, click Library Package Manager
and then Package Manager Console
. 从Tools菜单中,单击
Library Package Manager
,然后单击Package Manager Console
。
The enable-migrations
command creates a Migrations folder in the your project, and it puts in that folder a Configuration.cs file that you can edit to configure Migrations. enable-migrations
命令在项目中创建一个Migrations文件夹,并在该文件夹中放入一个Configuration.cs文件,您可以编辑该文件以配置迁移。
(If you missed the step above that directs you to change the database name, Migrations will find the existing database and automatically do the add-migration
command. That's OK, it just means you won't run a test of the migrations code before you deploy the database. Later when you run the update-database
command nothing will happen because the database will already exist.) (如果您错过了指示您更改数据库名称的上述步骤,则迁移将找到现有数据库并自动执行
add-migration
命令。这没关系,这只是意味着您不会在运行之前运行迁移代码测试部署数据库。稍后当您运行update-database
命令时,不会发生任何事情,因为数据库已经存在。)
more detail migration http://www.codeproject.com/Articles/801545/Code-First-Migrations-With-Entity-Framework 更多细节迁移http://www.codeproject.com/Articles/801545/Code-First-Migrations-With-Entity-Framework
another way : 其他方式 :
How to recreate database in EF if my model changes? 如果模型更改,如何在EF中重新创建数据库?
Note: if you use migration then do not lose data value most of time 注意:如果您使用迁移,则不要在大多数时间丢失数据值
problem in migration process run the flowing command 迁移过程中的问题运行流动命令
Add-Migration Initial -IgnoreChanges
Add-Migration Initial -IgnoreChanges
Update-Database -verbose
Update-Database -verbose
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.