升级到实体框架4.3.1后未处理的异常 [英] Unhandled Exception after Upgrading to Entity Framework 4.3.1

查看:118
本文介绍了升级到实体框架4.3.1后未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:

未处理的异常信息:System.Data.SqlClient.SqlException:操作失败,因为索引或名称统计IX_ID '已经存在于表'PrivateMakeUpLessons

Unhandled Exception: System.Data.SqlClient.SqlException: The operation failed because an index or statistics with name 'IX_ID' already exists on table 'PrivateMakeUpLessons'.

模型(简体,建设一个用于调试的独立测试项目):

public abstract class Lesson
{
    public Guid ID { get; set; }
    public string Room { get; set; }
    public TimeSpan Time { get; set; }
    public int Duration { get; set; }
}

public abstract class RecurringLesson : Lesson
{
    public int DayOfWeek { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Frequency { get; set; }
}

public class PrivateLesson : RecurringLesson
{
    public string Student { get; set; }
    public string Teacher { get; set; }
    public virtual ICollection<Cancellation> Cancellations { get; set; }
}

public class Cancellation
{
    public Guid ID { get; set; }
    public DateTime Date { get; set; }
    public virtual PrivateLesson Lesson { get; set; }
    public virtual MakeUpLesson MakeUpLesson { get; set; }
}

public class MakeUpLesson : Lesson
{
    public DateTime Date { get; set; }
    public string Teacher { get; set; }
    public virtual Cancellation Cancellation { get; set; }
}



配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Lesson>().ToTable("Lessons");
    modelBuilder.Entity<RecurringLesson>().ToTable("RecurringLessons");
    modelBuilder.Entity<PrivateLesson>().ToTable("PrivateLessons");
    modelBuilder.Entity<MakeUpLesson>().ToTable("PrivateMakeUpLessons");

    modelBuilder.Entity<Cancellation>()
        .HasOptional(x => x.MakeUpLesson)
        .WithRequired(x => x.Cancellation);

    base.OnModelCreating(modelBuilder);
}

备注

这在EF 4.2工作得很好。是不是有什么毛病我的模型?实际的模型要复杂得多,这就是为什么我所有类抽象出来。另外,我正在对现有的数据库,所以我需要使用表每类型继承。

This worked fine in EF 4.2. Is there something wrong with my model? The actual model is much more complicated which is why I have all the classes abstracted out. Also, I am working against an existing database so I need to use Table-Per-Type inheritance.

如果我更改的关系,取消 PrivateMakeUpLesson 1至0..1到0..1到0..1它的工作原理。这是不可取的,因为你不能有一个 PrivateMakeUpLesson 没有取消

If I change the relationship of Cancellation to PrivateMakeUpLesson from 1 to 0..1 to 0..1 to 0..1 it works. This is undesirable because you can't have a PrivateMakeUpLesson without a Cancellation.

另外,如果我让 PrivateMakeUpLesson 不会从课程继承那么它也适用,但它是一个教训,需要保持这样的现有业务逻辑。

Also, if I make PrivateMakeUpLesson NOT inherit from Lesson then it also works, but it IS a lesson and needs to remain so for existing business logic.

我会很感激的任何指导。 !谢谢

I'd appreciate any guidance. Thank you!

修改

开始一个赏金。我找不到关于索引生成代码第一EF 4.2和EF 4.3之间发生了什么变化的任何文件。很显然,EF 4.3是创造更多的索引和命名方案已更改,但我想知道,如果有一个在EF一个bug或者有什么根本性的错误我的模型或流畅API配置。

Starting a bounty. I can't find any documentation on what changed between EF 4.2 and EF 4.3 with regard to the index generation for code first. It's clear that EF 4.3 is creating more indexes and that the naming scheme has changed but I want to know if there's a bug in EF or if there is something fundamentally wrong with my model or fluent API configuration.

推荐答案

由于EF 4.3中,索引数据库创建过程中添加了freign键列。有可能会导致的索引被创建不止一次的错误。这将是固定在未来的版本EF

As of EF 4.3, indexes are added for freign key columns during database creation. There is a bug that can cause an index to be created more than once. This will be fixed in a future EF release.

在那之前,你可以解决这个问题通过的Migrations 而不是数据库初始化(或 Database.Create()法)。

Until then, you can work around the issue by creating your database using Migrations instead of database initializers (or the Database.Create() method).

生成初始迁移后,你将需要删除对<$ C $冗余呼叫C>指数()。

After generating the initial migration, you will need to delete the redundant call to Index().

CreateTable(
    "dbo.PrivateMakeUpLessons",
    c => new
        {
            ID = c.Guid(nullable: false),
            ...
        })
    .PrimaryKey(t => t.ID)
    .ForeignKey("dbo.Lessons", t => t.ID)
    .ForeignKey("dbo.Cancellations", t => t.ID)
    .Index(t => t.ID)
    .Index(t => t.ID); // <-- Remove this

要继续创建在运行时的数据库,你可以使用 MigrateDatabaseToLatestVersion 初始化。

To continue creating your database at run-time, you can use the MigrateDatabaseToLatestVersion initializer.

这篇关于升级到实体框架4.3.1后未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆