0

I am busy learning ASP.Net MVC and so I recently installed MVC 4 on Visual Studio 2010 Professional. I was trying this example:

http://www.luisrocha.net/2010/11/creating-composite-keys-using-code.html

But the RelatedTo attribute is not found by Visual Studio. I read on this question that this could be caused by Entity Framework 4.1 not being installed. I checked the version number of System.Data.Entity.dll and it gave version 4.0.0.0, runtime version v4.0.30319. So this seems to me to be version 4.0 of EF that is installed on my computer. I may be wrong on this though... so someone please correct me if I am.

I downloaded EF 4.1 from here and installed it, but the version numbers for System.Data.Entity.dll seem not to have changed and the RelatedTo attribute is still not working. Anyone know what is going wrong with the installation and how I could fix it?

UPDATE:

I installed Entity Framework using nuget as instructed on this site, and the console said it installed EntityFramework 5.0.0-rc, but the version of System.Data.Entity.dll seems to be unchanged.

No, I did not include using System.Data.Entity because intellisense did not tell me to include that. I did include System.ComponentModel.DataAnnotations and using System.ComponentModel.DataAnnotations.Schema because these were required by the Key and Column attributes. Intellisense asks me to "Generate class for 'RelatedTo'" and adding using System.Data.Entity does not change that.

Here is my code for those asking for it, although it is exactly the code from the tutorial that I linked to in the question:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace Example
{
    public class PlaylistTrack
    {
        [Key, Column(Order = 1)]
        public int PlaylistId { get; set; }

        [Key, Column(Order = 2)]
        public int TrackId { get; set; }

        [RelatedTo(ForeignKey = "PlaylistId")]
        public Playlist Playlist { get; set; }

        [RelatedTo(ForeignKey = "TrackId")]
        public Track Track { get; set; }
    }
}
Community
  • 1
  • 1
BruceHill
  • 6,954
  • 8
  • 62
  • 114

1 Answers1

4

Try installing new version of EF using Nuget. It will resolve all dependencies of EF and it will make sure you have the latest version. I don't know what is RelatedTo attribute supposed to be doing here. And see here in the data annotations section, the related to attribute is not in the EF4.1 release. But you can do the same thing like this.

      [ForeignKey("PlaylistId")] 
      public Playlist Playlist { get; set; }

      [ForeignKey("TrackId")] 
      public Track Track { get; set; }

EDIT RelatedTo attribute is replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC

Community
  • 1
  • 1
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • 1
    +1 For telling me about _ForeignKey_. Thanks, this attribute does seem to work. And it seems to be doing the same job as the _RelatedTo_. I read about RelatedTo over here: http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx. Can you (or anyone else) confirm that the _RelatedTo_ attribute has been replaced by _ForeignKey_? If that is so, then the tutorial that I linked to and the accepted answer to the question that I linked to are actually giving misinformation. – BruceHill May 24 '12 at 07:41