简体   繁体   English

实体框架的关系问题

[英]Relationship troubles with Entity Framework

I need help creating the relationship in entity framework as everything I have tried gives me errors when trying to add the migration or if I get passed that then I try to update the database and get an error about indexes with the same name. 我需要在实体框架中创建关系的帮助,因为尝试添加迁移时我尝试过的所有操作都会给我带来错误,或者如果我通过了该尝试,那么我会尝试更新数据库并获得有关同名索引的错误。

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

The problem with the above classes is that the OrginalInstance property on the Profile class and the LastKnownProfile in the Instance class are supposed to just be foreign keys to those specific tables and they probably won't be the same very often. 上述类的问题在于,Profile类的OrginalInstance属性和Instance类的LastKnownProfile应该只是这些特定表的外键,而且它们可能不会经常相同。 They can also both possibly be null. 它们也都可能为null。

I have tried: 我努力了:

modelBuilder.Entity<Instance>().HasRequired(i => i.LastKnownProfile);
modelBuilder.Entity<Profile>().HasRequired(p => p.OriginalInstance);

This gave me an Unable to determine the principal end of an association between the types 'EcuWeb.Data.Entities.Instance' and 'EcuWeb.Data.Entities.Profile'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 这使我Unable to determine the principal end of an association between the types 'EcuWeb.Data.Entities.Instance' and 'EcuWeb.Data.Entities.Profile'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. Unable to determine the principal end of an association between the types 'EcuWeb.Data.Entities.Instance' and 'EcuWeb.Data.Entities.Profile'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. error. 错误。

and with: 与:

modelBuilder.Entity<Instance>().HasRequired(i => i.LastKnownProfile).WithOptional();
modelBuilder.Entity<Profile>().HasRequired(p => p.OriginalInstance).WithOptional();

The database adds a foreign key reference back to itself. 数据库将外键引用添加回自身。

...that the OrginalInstance property on the Profile class and the LastKnownProfile in the Instance class are supposed to just be foreign keys to those specific tables and they probably won't be the same very often. ... Profile类别的OrginalInstance属性和Instance类别的LastKnownProfile应该只是这些特定表的外键,而且它们可能不会经常相同。 They can also both possibly be null. 它们也都可能为null。

In this case you actually want two one-to-many relationships between Profile and Instance if I don't misunderstand your quote above. 在这种情况下,如果我不误解您在上面的引用,您实际上需要ProfileInstance之间的一对多关系 It would mean that many Profiles can have the same OriginalInstance and that many Instances can have the same LastKnownProfile . 这意味着许多配置文件可以具有相同的OriginalInstance ,并且许多实例可以具有相同的LastKnownProfile The correct mapping would look like this then: 正确的映射如下所示:

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"));

The lines with MapKey are optional. 具有MapKey的行是可选的。 Without them EF will create a foreign key with a default name. 如果没有它们,EF将使用默认名称创建外键。

Also note that you must use HasOptional (instead of HasRequired ) if "both can possibly be null". 另请注意,如果“两者都可能为null”,则必须使用HasOptional (而不是HasRequired )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM