0

我正在尝试将旧网站转换为使用 ASP.Net 4.5 和 EF5 Code First,但是旧网站中有一个表我无法弄清楚如何转换,因为它有两列都引用了父表中的同一列. 谁能帮我转换这张表?

    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)
    )
4

2 回答 2

0

这就是使用属性,你也可以在没有属性的情况下做同样的事情,而是使用 FluentApi in OnModelCreating.

[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;}

}
于 2013-03-25T18:29:40.070 回答
0

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

[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; }
}
于 2013-03-25T19:52:34.367 回答