Entity Framework 5 does not generate [Key] attribute

I am using EF5 with VS2012 and I ran into a problem while creating tables.

1) I create an easy database schema (edmx) that contains 2 tables:

  • Table 1: Face
  • Attribute: Id (Guid)
  • Attribute: name (string)

  • Table 2: Role

  • Attribute: Id (Guid)
  • Attribute: Type (string)
  • Attribute: PersonId (Guid) (association with a table character)

2) that defines the properties of a unique key (called Id) as follows:

  • Concurrency Mode: None
  • Default value: None
  • Entity Key: True
  • Getter: Public
  • Name: Id
  • Nullable: False
  • Setter: Normal
  • StoreGeneratedPattern: Identity
  • Type: Guid (I also tried with Int32)

3) And the properties of the Entity model are:

  • Code Generation Strategy: None
  • Database Generation Workflow: TablePerTypeStrategy (VS)
  • Database Schema Name: dbo
  • DDL generation pattern: SSDLToSQL10.tt (VS)
  • Entity Container Access: Public
  • Entity Container Name: DatabaseSchemaContainer
  • Lazy Loading Enabled: True
  • Metadata artifact processing: built into output assembly
  • Namespace: DatabaseSchema
  • Pluralize new objects: False
  • Convert related text patterns to Save: True
  • Refresh Facets Facility: True
  • Validate On Build: True

4) related classes are created, but the [Key] attribute is missing:

using System; using System.Collections.Generic; public partial class Person { public Person() { this.Role = new HashSet<Role>(); } //[Key] <-- Missed! public System.Guid Id { get; set; } public string Name { get; set; } public virtual ICollection<Role> Role { get; set; } } 

5) I create a domain service to display the above objects to the client, for example:

 [EnableClientAccess] public class DomainService1 : DomainService { [Query] public IEnumerable<Person> GetPersonSet() { } [Update] public void UpdatePerson(Person person) { } [Insert] public void InsertPerson(Person person) { } [Delete] public void DeletePerson(Person person) { } } 

6) Recovering and receiving errors:

The "Person" object in the DomainService "DomainService1" does not have a specific key. Object types subject to DomainService actions must have at least one public property marked with KeyAttribute.

Any suggestion appreciated!

Ale

+4
source share
3 answers

You can try changing the code for the .tt generation template and adding attributes to the classes during generation. In your case, you can change the way you create properties as follows:

 public string Property(EdmProperty edmProperty) { return string.Format( CultureInfo.InvariantCulture, //Custom check for key field (_ef.IsKey(edmProperty) ? "[Key]" : "") + "{0} {1} {2} {{ {3}get; {4}set; }}", Accessibility.ForProperty(edmProperty), _typeMapper.GetTypeName(edmProperty.TypeUsage), _code.Escape(edmProperty), _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), _code.SpaceAfter(Accessibility.ForSetter(edmProperty))); } 

and add a namespace declaration to each generated class:

 public string UsingDirectives(bool inHeader, bool includeCollections = true) { return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) ? string.Format( CultureInfo.InvariantCulture, //Namespace for KeyAttribute goes here "{0}using System; using System.ComponentModel.DataAnnotations;{1}" + "{2}", inHeader ? Environment.NewLine : "", includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "", inHeader ? "" : Environment.NewLine) : ""; } 

After that, you can right-click on .tt and select "Run Custom Tool"

+6
source

Although this answer was originally from EF4, I suspect it is still like this:

T4 templates do not use data annotations because classes created from templates do not need them. ( source )

I suspect that this could also be due to the fact that you do not need KeyAttribute in the code first if you do not use key fields that do not follow conventions (for example, Id and PersonId are automatically used as keys for the Person object), if you don’t too far from the first route according to the model, perhaps use the code code first?

+1
source

vs2012 is not the right tool (yet?) for creating database models. You can better use visio (Not 2013!) Or SQL Server Management Studio. And you don’t need it, 99% of the data modeling work creates the right model, and not what Vs2012 is trying to automate a small step to C #.

That is why the obvious features as you need are not included in vs2012. And perhaps this is not possible, because vs2012 is for the first code developers.

0
source

All Articles