相关疑难解决方法(0)

实体框架代码优先迁移 - 不能删除约束,因为它不存在(命名约定从4.3到5.0)

以前使用EF 4.3并在升级到5.0时我发现索引,FK约束和PK约束都将其命名约定更改为包含dbo(例如PK_Users现在变为PK_dbo.Users)

现在,每当我对模型进行更改并且需要更改包含这些内容的表时,它总是说它不能删除约束,因为它无法找到它.

我只是想要它,以便当它试图删除约束/索引/键时,它首先检查5.0之前的命名是否存在,如果是,则删除它,但仍然使用新的5.0命名约定重新创建它.

命名约定从4.3更改为5.0:

主键/索引

Old: PK_Users                    New: PK_dbo.Users
Run Code Online (Sandbox Code Playgroud)

外键

Old: FK_Users_Roles_Role_Id      New: FK_dbo.Users_dbo.Roles_Role_Id               
Run Code Online (Sandbox Code Playgroud)

注意:我不能简单地让EF重新生成所有表,我在这个数据库中有生产数据.我也不想为每个使用自定义迁移的表手动执行此操作.

编辑:我发现了一个类似的问题如何停止添加dbo的Entity Framework 5迁移.成为关键名称?但是这个家伙只是想忽略5.0惯例并坚持使用4.3,它只处理表重命名.我不希望这样做,因为后续版本的EF可能会导致更多的更改,这些更改会影响此代码并且只是麻烦.

我试着按照发布​​的答案做同样的事情:

public class CodeMigrator : CSharpMigrationCodeGenerator
{
    protected override void Generate(
        DropIndexOperation dropIndexOperation, IndentedTextWriter writer)
    {
        dropIndexOperation.Name = StripDbo(dropIndexOperation.Name);
        base.Generate(dropIndexOperation, writer);
    }

    protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
    {
        dropForeignKeyOperation.Name = StripDbo(dropForeignKeyOperation.Name);
        base.Generate(dropForeignKeyOperation, writer);
    }

    protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer)
    {
        dropPrimaryKeyOperation.Name = StripDbo(dropPrimaryKeyOperation.Name);
        base.Generate(dropPrimaryKeyOperation, writer);
    }

    // TODO: Override other Generate overloads that involve table …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first ef-migrations entity-framework-5

12
推荐指数
1
解决办法
7129
查看次数

当键在基类中时,EF迁移DropForeignKey失败

我正在尝试更新EF 5.0.0-rc代码优先类库的数据模型.Up()方法中的第一个命令是

DropForeignKey("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" }, "dbo.ChileProducts");
Run Code Online (Sandbox Code Playgroud)

但我得到一个SqlException:'FK_dbo.ChileInventory_dbo.ChileProducts_LotInventoryItemTypeId_ChileProductId'不是约束.无法删除约束.

我认为错误的原因是因为我的ChileProducts类从它的基类得到了它的关键属性.由于DropForeignkey方法没有接受主列名称的重载,我相信EF无法确定要删除的正确约束.我还应该指出,异常消息中显示的约束名称与数据库中的约束名称不匹配(因此错误...)

您可以在下面找到数据模型和迁移方法.但首先要快速了解迁移背后的变化性质:InventoryBase类的每个衍生产品都通过InventoryTypeId和[InventoryTypeSpecific] ProductId的组合键定义产品类型.例如,ChileInventory会将其特定的智能类型类型识别为InventoryItemTypeId = [ChileInventoryTypeId]和ChileProductId = [ChileProductId].PackagingInventory会将其包装类型标识为InventoryItemTypeId = [PackagingInventoryTypeId]和PackagingProductId = [PackagingProductId].等等.

我正在努力推出的模型更改是将[InventoryTypeSpecific] ProductId从每个InventoryBase衍生产品升级到基础.这将导致所有InventoryBase派生对象共享一个公共ProductId属性,该属性与InventoryItemTypeId一起将启用从InventoryBase到ProductBase的导航; 这在以前的型号中是不可能的.

在此先感谢您的建议和考虑.--Vinney

数据模型

智利产品

public abstract class ProductBase
{
    [Key]
    [Column(Order = 0)]
    public virtual int InventoryItemTypeId { get; set; }

    [Key]
    [Column(Order = 1)]
    public virtual int Id { get; set; }

    [StringLength(150)]
    public virtual string Name { get; set; }

    [ForeignKey("InventoryItemTypeId")]
    public virtual InventoryItemType InventoryItemType { get; set; }

    public virtual bool IsActive { get; …
Run Code Online (Sandbox Code Playgroud)

ef-migrations entity-framework-5

6
推荐指数
2
解决办法
5965
查看次数

表'DBNAME.dbo.TableNAME'不存在与MySQL的实体框架6

我正在使用Entity Framework 6.0.0和MySql Server 5.6.17

我通过nuget添加了MySql.Data.Entities,它安装了Entity Framework 6.0.0和MySql.Data 6.8.4

一切都设置完美,并与我的一些商业实体合作.它启用了自动迁移(true).

后来我又添加了一些实体,然后开始给出错误

Table 'DBNAME.dbo.TABLENAME' doesn't exist Entity Framework 6
Run Code Online (Sandbox Code Playgroud)

我已经尝试删除整个数据库并重新创建它,但它没有奏效.

我已经尝试将实体框架更新到6.1.2和MySql.Data更新到6.9.5但它没有解决问题但是给出了一些其他错误

Method not found: 'System.Data.Entity.Migrations.Builders.TableBuilder`1<!0> System.Data.Entity.Migrations.Builders.TableBuilder`1.Index(System.Linq.Expressions.Expression`1<System.Func`2<!0,System.Object>>, System.String, Boolean, Boolean, System.Object)'.
Run Code Online (Sandbox Code Playgroud)

所以我将我的EF和MySql.Data更改为以前的版本(EF 6.0.0和MySql.Data 6.8.4)

我发现另外一篇文章http://bugs.mysql.com/bug.php?id=69649有像我这样的错误,所以我修改了我的配置方法如下

 public Configuration()
 {
            this.AutomaticMigrationsEnabled = true;
            SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
            CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();
            AutomaticMigrationDataLossAllowed = true;  // or false in case data loss is not allowed.
 }
Run Code Online (Sandbox Code Playgroud)

但它没有解决问题.我又得到了同样的错误.

我的样本业务实体如下.

public class User
{
    [Key]
    public int UserID { get; set; }
    [Display(Name = "User Email")] …
Run Code Online (Sandbox Code Playgroud)

c# mysql entity-framework entity-framework-6

3
推荐指数
1
解决办法
8986
查看次数