0

I have a class named 'Authority' in a library that created by Database First method

As we know Database First generate all the classes automatically also Primary Key defined for each class, I also created 'User' class has a property of Authority class, in another library I have 'PacsUser' class derived from 'User' I am using code first approach to develop 'PacsUser' when I want PacsUser to return a specific user it gives me "EntityType 'Authority' has no key defined"

I have to define key for Authority class in OnModelCreating of PacsUserDBContext like:

modelBuilder.Entity<Authority>().HasKey(e => e.AID);

why DbModelBuilder in PacsUserDBContext recognize Authority's primary key automatically? or at least by

modelBuilder.Entity<Authority>().ToTable("Authority");

all things work properly (just for example).

They both(Authority bay DB-First and PacsUser by Code-First) are using Entity Framework ORM so

Why Code first doesn't know Authority primary key ?

Is there a way DbModelBuilder recognize primary keys automatically ?

Any Idea would be appreciated.

2
  • 2
    for this your column names should follow EF convention. For a PK: ID or ClassNameID. Here AID should be ID or AuthorityID. From ef6, custom conventions are allowed.
    – tschmit007
    Jul 23, 2014 at 12:43
  • Thanks, your comment could be my answer.
    – Aria
    Aug 2, 2014 at 5:26

1 Answer 1

1

Primary Key Convention Code First infers that a property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column.

For example if you have following class

public class Department 
{ 
   // Primary key 
   public int DepartmentID { get; set; } 

    . . .  

}

EF will use the DepartmentID as primary key, as alternative you could use Id Instead DepartmentID. But your Authority class's key(AID) doesn't follow above convention. so you have to define custom mapping for it or change the AID to Id or AuthorityId.

You could see more default EF conventions here

3
  • Thanks, I thought Code-First can recognize primary key attribute in DB-First as I wrote [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] , I can change AID to AuthorityId and it will work properly but how about backward compatibility? do you think that this is a weakness for EF designer team?
    – Aria
    Aug 2, 2014 at 5:36
  • @aria: you can use modelBuilder.Entity<Authority>().Property(x=>x.AuthorityId).HasColumnName("AID")) for backward compatibility.
    – Masoud
    Aug 2, 2014 at 6:03
  • cause of my change my app doesn't work for emaxple I have a lot of serializes of Authority class and I need AID therefore your code can not help about backward compatibility, but what is your opinion about this weakness?
    – Aria
    Aug 2, 2014 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.