C# 先将SQL表转换为ASP.NETEF5代码的实体

C# 先将SQL表转换为ASP.NETEF5代码的实体,c#,ef-code-first,entity-framework-5,asp.net-4.5,C#,Ef Code First,Entity Framework 5,Asp.net 4.5,我正在尝试先将一个旧网站转换为使用ASP.NET4.5和EF5代码,但旧网站中有一个表我无法确定如何转换,因为它有两个列,两个列都引用其父表中的同一列。谁能帮我把这张桌子换一下吗 CREATE TABLE dbo.tblCollectionsList ( ListID smallint IDENTITY(0,1) NOT NULL, SectionID smallint NOT NULL, IncludedSectionID sma

我正在尝试先将一个旧网站转换为使用ASP.NET4.5和EF5代码,但旧网站中有一个表我无法确定如何转换,因为它有两个列,两个列都引用其父表中的同一列。谁能帮我把这张桌子换一下吗

    CREATE TABLE dbo.tblCollectionsList
    (
        ListID smallint IDENTITY(0,1) NOT NULL,
        SectionID smallint NOT NULL,
        IncludedSectionID smallint NOT NULL,
        CONSTRAINT PK_CollectionsList_ListID PRIMARY KEY CLUSTERED (ListID ASC),
        CONSTRAINT FK_CollectionList_SectionInfo FOREIGN KEY (SectionID) REFERENCES dbo.tblSectionInfo (SectionID),
        CONSTRAINT FK_CollectionList_SectionInfo2 FOREIGN KEY (IncludedSectionID) REFERENCES dbo.tblSectionInfo (SectionID)
    )

这就是使用属性,您也可以在不使用属性的情况下执行相同的操作,而是在
OnModelCreating
中使用FluentApi

[Table("tblCollectionsList")]
public class CollectionsList
{
[Key]
public byte ListID {get;set;}
public byte SectionID {get;set;}
public byte IncludedSectionID {get;set;}

[ForeignKey("SectionID")]
public virtual Section Section {get;set;}
[ForeignKey("IncludedSectionID")]
public virtual Section IncludedSection {get;set;}

}

请原谅个人对表名的偏好,但您面临的问题是级联删除,您可以在模型创建中关闭此行为:

[Table("Section")]
public class Section
{
    [Key]
    public virtual int Id { get; set; }
    public virtual string Description { get; set; }
}

[Table("List")]
public class List
{
    [Key]
    public virtual int Id { get; set; }
    [Required]
    [ForeignKey("Section")]
    public virtual int SectionId { get; set; }
    public virtual Section Section { get; set; }
    [Required]
    [ForeignKey("IncludedSection")]
    public virtual int IncludedSectionId { get; set; }
    public virtual Section IncludedSection { get; set; }
}

public class SectionContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();   
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<Section> Sections { get; set; }
    public DbSet<List> Lists { get; set; }
}
[表(“节”)]
公共课组
{
[关键]
公共虚拟整数Id{get;set;}
公共虚拟字符串描述{get;set;}
}
[表(“清单”)]
公共班级名单
{
[关键]
公共虚拟整数Id{get;set;}
[必需]
[外汇(“部分”)]
公共虚拟int SectionId{get;set;}
公共虚拟段{get;set;}
[必需]
[外键(“包含部分”)]
公共虚拟int IncludedSectionId{get;set;}
公共虚拟节IncludedSection{get;set;}
}
公共类SectionContext:DbContext
{
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
基于模型创建(modelBuilder);
}
公共DbSet节{get;set;}
公共数据库集列表{get;set;}
}

我实际上已经尝试过了,但出现了错误:在表“CollectionItem”上引入外键约束“FK_dbo.CollectionItem_dbo.Section_IncludedSectionID”可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他外键约束。禁用此选项将如何影响其他表和删除?这也会给自动迁移带来任何问题吗?只要您不依赖级联删除(作为个人策略,我不建议以任何方式利用级联删除),它就不会带来任何问题。我只在开发/集成环境中使用自动迁移,因此这不会带来任何问题,因为数据通常不等同于生产。(澄清一下,在大型系统中,最好使用第三方工具将您的开发环境和更改脚本化为文件,以便部署到生产环境中,并在暂存中进行测试,而不是依赖自动迁移。)