C# 实体框架的关系问题

C# 实体框架的关系问题,c#,entity-framework,C#,Entity Framework,我需要在entity framework中创建关系的帮助,因为我尝试的所有操作都会在尝试添加迁移时出错,或者如果我通过了迁移,那么我会尝试更新数据库,并获得关于同名索引的错误 public class Profile { public Profile() { Environments = new HashSet<Environment>(); } [Key] public int Id { get; set; } p

我需要在entity framework中创建关系的帮助,因为我尝试的所有操作都会在尝试添加迁移时出错,或者如果我通过了迁移,那么我会尝试更新数据库,并获得关于同名索引的错误

public class Profile
{
    public Profile()
    {
        Environments = new HashSet<Environment>();
    }

    [Key]
    public int Id { get; set; }

    public string VersionCreated { get; set; }

    public string DiskLocation { get; set; }

    public string Name { get; set; }

    public DateTime DateTime { get; set; }

    public virtual Product Product { get; set; }

    public virtual Instance OriginalInstance { get; set; }

    public virtual ICollection<Environment> Environments { get; set; } 
}


public class Instance
{
    public Instance()
    {
        TestResults = new HashSet<TestResult>();
        Environments = new HashSet<Environment>();
    }

    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Version { get; set; }

    public string UserFriendlyName { get; set; }

    public virtual Product Product { get; set; }

    public virtual Profile LastKnownProfile { get; set; }

    public virtual Computer Computer { get; set; }

    public virtual ICollection<TestResult> TestResults { get; set; }

    public virtual ICollection<Environment> Environments { get; set; } 
}
数据库将外键引用添加回自身

…配置文件类上的OrginalInstance属性和 实例类中的LastKnownProfile应该是外来的 这些特定表的键,它们可能不太一样 经常它们也可能都为null

在这种情况下,如果我没有误解您上面的引用,您实际上希望
Profile
Instance
之间存在两个一对多的关系。这意味着许多配置文件可以具有相同的
原始状态
,并且许多实例可以具有相同的
LastKnown配置文件
。正确的映射如下所示:

modelBuilder.Entity<Profile>()
    .HasOptional(p => p.OriginalInstance)
    .WithMany()
    .Map(m => m.MapKey("OriginalInstanceId"));

modelBuilder.Entity<Instance>()
    .HasOptional(i => i.LastKnownProfile)
    .WithMany()
    .Map(m => m.MapKey("LastKnownProfileId"));
modelBuilder.Entity()
.has可选(p=>p.OriginalInstance)
.有很多
.Map(m=>m.MapKey(“OriginalInstanceId”);
modelBuilder.Entity()
.has可选(i=>i.lastKnown配置文件)
.有很多
.Map(m=>m.MapKey(“lastnownprofileid”);
带有
MapKey
的行是可选的。如果没有它们,EF将创建一个具有默认名称的外键

还请注意,如果“两者都可能为空”,则必须使用
HasOptional
(而不是
HasRequired

…配置文件类上的OrginalInstance属性和 实例类中的LastKnownProfile应该是外来的 这些特定表的键,它们可能不太一样 经常它们也可能都为null

在这种情况下,如果我没有误解您上面的引用,您实际上希望
Profile
Instance
之间存在两个一对多的关系。这意味着许多配置文件可以具有相同的
原始状态
,并且许多实例可以具有相同的
LastKnown配置文件
。正确的映射如下所示:

modelBuilder.Entity<Profile>()
    .HasOptional(p => p.OriginalInstance)
    .WithMany()
    .Map(m => m.MapKey("OriginalInstanceId"));

modelBuilder.Entity<Instance>()
    .HasOptional(i => i.LastKnownProfile)
    .WithMany()
    .Map(m => m.MapKey("LastKnownProfileId"));
modelBuilder.Entity()
.has可选(p=>p.OriginalInstance)
.有很多
.Map(m=>m.MapKey(“OriginalInstanceId”);
modelBuilder.Entity()
.has可选(i=>i.lastKnown配置文件)
.有很多
.Map(m=>m.MapKey(“lastnownprofileid”);
带有
MapKey
的行是可选的。如果没有它们,EF将创建一个具有默认名称的外键


还请注意,如果“两者都可能为空”,则必须使用
HasOptional
(而不是
HasRequired
)。

似乎您希望这两个实体之间有一对一的关系,请尝试仅使用一个映射命令:modelBuilder.Entity().HasRequired(i=>i.LastKnownProfile)。WithOptional(p=>p.OriginalInstance);这是更接近,但这不是我所寻找的,因为这两个属性根本不相关。他们可以而且可能会几乎每次都不一样。它们应该只是相应表的外键,而不与其他属性相关,例如LastKnownProfile应该包含ProfileId,OriginalInstance应该包含不同的InstanceId,而不是包含LastKnownProfile的InstanceId。似乎您希望这两个实体之间有一对一的关系,尝试仅使用一个映射命令:modelBuilder.Entity().HasRequired(i=>i.lastnownprofile)。with可选(p=>p.OriginalInstance);这是更接近,但这不是我所寻找的,因为这两个属性根本不相关。他们可以而且可能会几乎每次都不一样。它们应该只是相应表的外键,而不与其他属性相关,例如LastKnownProfile应该包含ProfileId,OriginalInstance应该包含不同的InstanceId,而不是包含LastKnownProfile的InstanceId。
modelBuilder.Entity<Profile>()
    .HasOptional(p => p.OriginalInstance)
    .WithMany()
    .Map(m => m.MapKey("OriginalInstanceId"));

modelBuilder.Entity<Instance>()
    .HasOptional(i => i.LastKnownProfile)
    .WithMany()
    .Map(m => m.MapKey("LastKnownProfileId"));