【问题标题】:entity framework 6 code first change mapping from view to new table实体框架 6 代码首先更改从视图到新表的映射
【发布时间】:2015-02-02 02:40:10
【问题描述】:

我正在维护一个 WPF 应用程序,它使用 Entity Framework Code First 和迁移(不是自动)。此应用程序中的一些 POCO 映射到视图(指向另一个数据库)。我猜用于执行此操作的方法类似于此答案中所做的:how to use views in code first entity framework

现在我希望其中一个 POCO(公司)指向一个表而不是视图。指向视图后,POCO 也发生了一些变化。

当我在 POCO 和 CompanyConfiguration 类(更改 ToTable())中进行更改后添加迁移时,迁移似乎认为视图是现有表并尝试重命名它。例如,Up() 方法的开始将如下所示:

    RenameTable(name: "dbo.vCompany", newName: "Company");

    AlterColumn("dbo.Company", "ParentAccount", c => c.String(maxLength: 160));
    AlterColumn("dbo.Company", "Country", c => c.String(maxLength: 100));

但是,我不希望更改视图,我希望从头开始创建与 POCO 匹配的公司表。实现此目的的正确/好方法是什么?自己不写 Up() 和 Down() 方法可以吗?

目前该应用程序使用 Entity Framework 6.1,但当第一次创建此 Company/vCompany 映射时,我相信它是 4.3 版。

【问题讨论】:

    标签: c# wpf entity-framework view code-first


    【解决方案1】:

    没有太多的信息/帮助来解决这个问题,但我做了以下对我有用的事情(尽管工作非常繁琐):

    我不得不完全重写迁移中自动创建的 Up() 和 Down() 方法。使用 CreateTable() 创建表而不是重命名视图。

    在 Up() 方法中:

    CreateTable(
                "dbo.Company",
                c => new
                {
                    CompanyId = c.Guid(nullable:false),
                    ParentAccount = c.String(maxLength:160),
                    Country = c.String(maxLength:100),
                    Address1_City = c.String(maxLength:4000),
                    Address1_Country = c.String(maxLength: 4000),
                    Address1_Line1 = c.String(maxLength: 4000),
                    Address1_Line2 = c.String(maxLength: 4000),
                    Address1_PostalCode = c.String(maxLength:50),
                    Owner = c.String(maxLength:160),
                    OwnerId = c.Guid(nullable:false),
                    Name = c.String(maxLength:160),
                    EMailAddress = c.String(maxLength:100),
                    InvoiceEMailAddress = c.String(maxLength:100),
                    Fax = c.String(maxLength:50),
                    CreditLimit = c.Decimal(storeType:"money"),
                    CreditOnHold = c.Boolean(nullable:false, defaultValue:false),
                    IsPrivate = c.Boolean(nullable:false, defaultValue:false),
                    StatusCode = c.Int(),
                    CustomerTypeCode = c.Int(),
                    BEGreenCreditLimit = c.Int(),
                    IssuingBodyAccount = c.String(maxLength:20),
                    CustomerType = c.String(maxLength:100),
                    IsElProducer = c.Boolean(nullable:false, defaultValue:false),
                    IsEnergyTrader = c.Boolean(nullable:false,defaultValue:false),
                    VATNumber = c.String(maxLength:100),
                    ContactPerson = c.String(maxLength:160),
                    ContactPersonPhone = c.String(maxLength:50),
                    ContactPersonFax = c.String(maxLength:50),
                    ContactPersonEmail = c.String(maxLength:100)
    
                }
                )
                .PrimaryKey(p=>p.CompanyId)
                ;
    

    从另一个数据库中提取的公司值,参考某些列的 long maxLength。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      相关资源
      最近更新 更多