加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 升级到实体框架后未处理的异常4.3.1

发布时间:2020-12-15 18:10:21 所属栏目:百科 来源:网络整理
导读:错误: 未处理的异常:System.Data.SqlClient.SqlException:操作失败,因为表’PrivateMakeUpLessons’上已存在名称为“IX_ID”的索引或统计信息. 模型(简化,构建在单独的测试项目中进行调试): public abstract class Lesson{ public Guid ID { get; set; }
错误:

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

模型(简化,构建在单独的测试项目中进行调试):

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中很好.我的模型有什么问题吗?实际的模型要复杂得多,这就是为什么我把所有的类都抽象出来.此外,我正在处理一个现有的数据库,所以我需要使用Table-Per-Type继承.

如果我将取消与PrivateMakeUpLesson的关系从1更改为0..1到0..1到0..1它可以工作.这是不希望的,因为您无法使用PrivateMakeUpLesson而无需取消.

此外,如果我使PrivateMakeUpLesson不继承Lesson,那么它也可以工作,但它是一个教训,需要保持现有的业务逻辑.

我会感谢任何指导.谢谢!

编辑:

开始赏金我没有找到有关EF 4.2和EF 4.3之间关于代码优先索引生成发生变化的任何文档.很明显,EF 4.3正在创建更多的索引,命名方案已经改变,但是我想知道EF中是否有错误,或者我的模型或流畅的API配置有什么根本的错误.

解决方法

在EF 4.3之前,在数据库创建过程中为freign键列添加索引.有一个错误可能导致不止一次创建索引.这将在未来的EF版本中修复.

在此之前,您可以通过使用Migrations而不是数据库初始化程序(或Database.Create()方法)创建数据库来解决此问题.

生成初始迁移后,您将需要删除对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初始化程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读