【问题标题】:How to create one to one foreign key using FluentMigrator如何使用 FluentMigrator 创建一对一的外键
【发布时间】:2019-09-11 11:08:45
【问题描述】:

我正在使用此代码来创建外键:

Create.ForeignKey("FK_Table1_Table2")
    .FromTable("Table1").ForeignColumn("Table1_ID")
    .ToTable("Table2").PrimaryColumn("Table2_ID");

因此我有一对多的关系:

public class Table1
{
    public int Table1_ID { get; set; }
    public virtual Table2 Table2 { get; set; }
}


public class Table2
{
    public Table2()
    {
        this.Tables1 = new HashSet<Table1>();
    }
    public int Table2_ID { get; set; }
    public virtual ICollection<Table1> Tables1 { get; set; }
}

但我想看看:

public class Table2
{
    public int Table2_ID { get; set; }
    public virtual Table1 Table1 { get; set; }
}

有没有办法使用 FluentMigrator 做到这一点?

【问题讨论】:

    标签: c# fluent-migrator db-first


    【解决方案1】:

    关系数据库对于多/一对一 FK 关系没有特殊的概念。 FK 成为“一”的唯一方法是它也具有唯一约束。

    所以我相信你应该在外键创建代码之后添加这样的东西:

    Create.UniqueConstraint()
        .OnTable("Table1").Column("Table1_ID");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多