0

So I changed up my databases and remade them. I followed the EF6 tutorial but encountered this error when trying to create a controller with views. I did some research on the error and people said to add data annotations and I tried [Key] but I keep getting the same error. Not sure if i've forgotten anything? Thanks!

"There was an error running the selected code generator: 'Unable to retrieve metadata for 'LinkDB.Models.Identifier.' Unable to determine the principal end of an association between the type 'LinkDB.Models.Identifier' and 'LinkDB.Models.Identity'. The principal end of this association must be explicity configured using either the relationship fluent API or data annotation.'

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace LinksDB.Models
{
    public class Identity
    {
        [Key]
        public int ID { get; set; }

        public int IdentifierID { get; set; }
        public string contact { get; set; }
        public string contactname { get; set; }
        public string price { get; set; }

        public virtual ICollection<Link> Links { get; set; }
        public virtual Identifier Identifiers { get; set; }
        public virtual Metric Metrics { get; set; }
    }
}

 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;


namespace LinksDB.Models
{
    public class Identifier
    {
        [Key]
        public int ID { get; set; }
        public string domain { get; set; }

        public virtual ICollection<Link> Links { get; set; }
        public virtual Identity Identitys { get; set; }
        public virtual Metric Metrics { get; set; }

    }
}

using LinksDB.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace LinksDB.DAL
{
    public class LinkData : DbContext
    {

        public LinkData() : base("LinkData")
        {
        }
        public DbSet<Identifier> Identifiers { get; set; }
        public DbSet<Identity> Identitys { get; set; }
        public DbSet<Link> Links { get; set; }
        public DbSet<Metric> Metrics { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
liamcook
  • 143
  • 2
  • 10
  • @kirk have just added it, thanks! – liamcook Oct 18 '17 at 20:52
  • You don't really need the [Key] if you follow the convention of calling your key `ID `or `Class + ID` which you do. Problem lies elsewhere. Is the line `public virtual Identity Identitys { get; set; }` in Identity class correct (self reference) or did you intend that to be Identifier (paired automatically with IdentifierId) FK? – Steve Greene Oct 18 '17 at 21:12
  • @SteveGreene I thought that as well, originally I had it as its own key so MetricID and IdentityID. Granted my databases aren't named greatly it was my first time at kind of mapping them! But I've intended to add an identifierID to each database so the data from each databases can be collated if that makes sense? Where else could the problem lie I have gone through the whole build! Thanks – liamcook Oct 19 '17 at 08:08
  • Start with your Identity model. You have what I assume is a FK named IdentifierID, but no corresponding Identifier nav property. Then you have it pointing at itself with the Identitys nav property. Is this your intention? Also, what command throws the error? – Steve Greene Oct 19 '17 at 13:45
  • @SteveGreene ahh you're right i didn't catch that, the issue with naming them both so closely! Its when i create a controller with views with the identifier database as the model class – liamcook Oct 20 '17 at 11:07
  • @SteveGreene Hey don't know if you're still following this post anymore! I did what you said about the thing, and now I'm getting another error. I'm getting "There was an error running the selected code generator: 'Unable to retrieve metadata for 'LinkDB.Models.Identifier.' Unable to determine the principal end of an association between the type 'LinkDB.Models.Identifier' and 'LinkDB.Models.Identity'. The principal end of this association must be explicity configured using either the relationship fluent API or data annotation.' – liamcook Oct 22 '17 at 21:43
  • Your Identity model as posted above is incorrect. Post your Identity and Identifier models. – Steve Greene Oct 23 '17 at 02:17
  • @SteveGreene Have corrected them now, any ideas? – liamcook Oct 23 '17 at 13:34
  • What is the desired relationship between identifier and identity? For example, is identifier optional in indentity or are both ends required? If both ends are required, they must share a PK. See [here](https://stackoverflow.com/questions/24605152/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c). – Steve Greene Oct 23 '17 at 13:50
  • @SteveGreene I'm a massive novice still so i tried to make sense of that post. In reality they would be inter-dependant so a 1:1 relationship. The identifier just contains the ID and the domain, identity contains the email of the domain, their name and price (for a link). I would like to different views where i have ID, domain then the identity variables. Then another view with ID, domain and then metric variables. So i thought separating the tables as much as possible and having identifierID as the FK would allow it to fetch as little information as possible. Thanks for your time! – liamcook Oct 23 '17 at 14:27

1 Answers1

0

OK, if you want a 1:1 relationship between Identity and Identifier those models should look like below where the child (Indentifier) uses the IdentityId as both it's PK and FK. You can also do this with fluent code. Start with this and then add in your Metric and Links relationships.

public class Identity
{
    [Key]
    public int ID { get; set; }

    public string contact { get; set; }
    public string contactname { get; set; }
    public string price { get; set; }

    public virtual Identifier Identifier { get; set; }
}

public class Identifier
{
    [Key, ForeignKey("Identity")]
    public int IdentityID { get; set; }

    public string domain { get; set; }

    public virtual Identity Identity { get; set; }
}

Example here

Steve Greene
  • 12,029
  • 1
  • 33
  • 54