Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Created Issue: EF does not pick up keys from base unmapped types by "TypeNameId" convention [1591]

$
0
0
Consider the following:

```
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

public class Instructor : Person
{
public DateTime HireDate { get; set; }
}

public class MyContext : DbContext
{
public DbSet<Instructor> Instructors { get; set; }
}

```

This will throw the following exception:
EntityType 'Instructor' has no key defined. Define the key for this EntityType. Instructors: EntityType: EntitySet 'Instructors' is based on type 'Instructor' that has no keys defined.

This happens because Person is unmapped, and we don't pick up the key information by convention from it. When looking at properties of Instructor, we don't find key either, because we look for Id or InstructorId.

This can be worked-around easily, by changing the key property to Id, or decorating it with Key attribute, mapping Person instead, or using fluent API.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>