twreid
twreid

Reputation: 1453

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. They can also both possibly be 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. 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.

Upvotes: 1

Views: 144

Answers (1)

Slauma
Slauma

Reputation: 177163

...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. They can also both possibly be null.

In this case you actually want two one-to-many relationships between Profile and Instance if I don't misunderstand your quote above. It would mean that many Profiles can have the same OriginalInstance and that many Instances can have the same 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. Without them EF will create a foreign key with a default name.

Also note that you must use HasOptional (instead of HasRequired) if "both can possibly be null".

Upvotes: 1

Related Questions