20119

Entity Framework modeling with .NET4, Data Annotations issue

Question:

I am trying to use Entity Framework on my web site. I followed tutorials and Visual Studio (2010) does not complain. However, when I add a ForeignKey data annotation, I get an error when I run on the web site that the Namespace does not exist. CS0246: The type or namespace name 'ForeignKey' could not be found (are you missing a using directive or an assembly reference?)

After doing some searching, it appears that the ForeignKey data annotation does not exist in .NET 4.

If that is the case, is there a way to add at foreign key to the model in .NET 4?

Answer1:

Correct, System.ComponentModel.DataAnnotations.Schema is part of .NET 4.5.

Have you instead looked at the Fluent API?

Here's a video for VS 2010 specifically. <a href="http://msdn.microsoft.com/en-ca/data/gg194009.aspx" rel="nofollow">http://msdn.microsoft.com/en-ca/data/gg194009.aspx</a>

Recommend

  • Can't find extarnal classes in Mono mcs C#
  • adding custom .dll class library to asp.net/C# website
  • “are you missing a using directive or assembly reference?” Am I or am I not?
  • .NET project with project.json cannot be built
  • Need help to stop program terminating without users consent
  • PHP Handling Namespace with SimpleXML
  • Finding All Combinations (cartesian product) of list values in vb.net
  • How to add System.Windows dll to Visual Studio 2010 express?
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • WPF Template Binding in ToggleButton UserControl
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • git add error : “fatal : malloc, out of memory”
  • How to autopopulate a field in SugarCRM form
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Differences in dis-assembled C code of GCC and Borland?
  • What is the purpose of TaskExecutor in spring?
  • Xcode 4 NSLog Macro link in Xcode 3
  • Extracting HTML between tags
  • Get one-time binding to work for ng-if
  • Is there any way to access browser form field suggestions from JavaScript?
  • javaw.exe and eclipse startup problems
  • Build own AppleScript numerical error handling
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Why joiner is not used after Sequence generator or Update statergy
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Checking variable from a different class in C#
  • Binding checkboxes to object values in AngularJs