多个自引用导航属性

时间:2016-07-03 20:32:22

标签: c# entity-framework code-first self-reference

我们销售的产品为我们颁发许可证编号,并且客户可以每年升级。我想设置License POCO,通过定义UpgradedToUpgradedFrom导航属性来跟踪此升级信息,这样我们就可以轻松地向上/向下移动“链”相关许可证。所以基本上类似于以下内容:

public class License
{
    [Key]
    public string LicenseNum { get; set; }
    // Other properties relating to license omitted...

    // Optional relationship.
    public License UpgradedTo { get; set; }

    // Optional relationship.
    public License UpgradedFrom { get; set; }
}

我真的很难用EF Annotations和Fluent API来定义它。我认为自我引用方面正在绊倒我。

我们还希望能够在给定UpgradeTo上设置其中一个UpgradeFrom / License属性,并让EF处理“对面”升级属性关系的另一端。如下所示:

// Licenses upgraded 1 > 2 > 3
License lic1 = CreateLicense('1');
License lic2 = CreateLicense('2');
License lic3 = CreateLicense('3');

using (var db = new Model1())
{
    // Insert into database
    db.Licenses.Add(lic1);
    db.Licenses.Add(lic2);
    db.Licenses.Add(lic3);
    db.SaveChanges();

    // Specify UpgradeFrom/UpgradeTo info only on lic2.
    lic2.UpgradedFrom = lic1;
    lic2.UpgradedTo = lic3;
    db.SaveChanges();

    // lic1 and lic3 automatically update possible?
    Debug.Assert(lic1.UpgradedTo == lic2);
    Debug.Assert(lic3.UpgradedFrom == lic2);
}

1 个答案:

答案 0 :(得分:0)

这种情况非常棘手,因为依赖性如何起作用。

诀窍是添加一个或多个额外的“假”属性来完成工作。

如果设置了UpgradeTo值,此类将自动设置UpgradedFrom属性。

示例:

using (var ctx = new TestContext2())
{
    var license1 = ctx.Licenses.Add(new License() { LicenseNum = "1.0.0"});
    ctx.SaveChanges();

    var license2 = license1.UpgradeTo = new License() { LicenseNum = "1.0.2"};
    ctx.SaveChanges();

    var license3 = license2.UpgradeTo = new License() { LicenseNum = "1.0.3" };
    ctx.SaveChanges();
}

<强>实体

public class License
{
    [Key]
    public string LicenseNum { get; set; }

    private License _upgradeTo;
    private License _upgradedFrom;


    public License UpgradeTo
    {
        get { return _upgradeTo; }
        set
        {
            _upgradeTo = value;
            if (_upgradeTo != null && _upgradeTo.UpgradedFrom != this)
            {
                _upgradeTo.UpgradedFrom = this;
            }
        }
    }

    public License UpgradedFrom
    {
        get { return _upgradedFrom; }
        set
        {
            _upgradedFrom = value;
            if (_upgradedFrom != null && _upgradedFrom.UpgradeTo != this)
            {
                _upgradedFrom.UpgradeTo = this;
            }
        }
    }

    internal License InternalUpgradedTo
    {
        get { return UpgradeTo; }
    }

    internal License InternalUpgradedFrom
    {
        get { return UpgradedFrom; }
    }
}

<强>上下文

public  class TestContext2 : DbContext
{
    public TestContext2() : base(My.Config.ConnectionStrings.TestDatabase)
    {

    }
    public DbSet<License> Licenses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<License>()
            .HasOptional(v => v.UpgradeTo)
            .WithOptionalDependent(x => x.InternalUpgradedFrom);

        modelBuilder.Entity<License>()
            .HasOptional(v => v.UpgradedFrom)
            .WithOptionalDependent(x => x.InternalUpgradedTo);
    }
}