简体   繁体   English

使用Entity Framework将模型映射到现有数据库表

[英]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 注意:如果您使用迁移,则不要在大多数时间丢失数据值

Update: 更新:

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.

相关问题 如何在现有的远程数据库中添加表并将其链接到Entity Framework模型? - How to add table in existing remote database and link it to Entity Framework model? 使用Entity Framework手动将POCO映射到现有表 - Manually map POCO to existing Table using Entity Framework 对现有数据库使用实体框架。 防止表删除? - Using Entity Framework with an existing database. Prevent Table Deletion? 实体框架6从现有表创建模型 - Entity Framework 6 Create model from existing table 将不带键的现有表添加到实体框架模型 - Adding an existing table with no Key to an Entity Framework model 从Entity Framework中的现有表创建模型 - Creating model from existing table in Entity Framework 如何将基本表添加到使用Entity Framework创建TPH模型的现有实体? - How can I add a base table to an existing entity creating TPH model using Entity Framework? 实体框架将实体对象映射到现有表AspNetUsers - Entity Framework map entity object to existing table AspNetUsers 如何使用Entity Framework将POCO模型类映射到现有数据库表 - How do I map POCO model classes to existing DB tables using Entity Framework 使用实体框架在创建新实体时向数据库中的现有表添加新记录 - Add a new record to existing table in the database on a new entity creation using Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM