问题
I used nuget to upgrade EF5 to EF6, and somewhere a breaking change was introduced for my solution. I discovered this when running one of my unit tests (it affects everything though). In each test, I init by doing this:
// warm up EF.
using (var context = new ReportingDbContext())
{
context.Database.Initialize(false); // <-- boom!
}
// init the service
_inventoryService = new InventoryService();
It tosses me this exception:
The property 'EmployeeID' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type.
The strange thing this, everything was just peachy on EF5. I went hunting through my models (I have a bunch) and found everywhere that EmployeeID lives. They all look like this:
[Table("mytablename")]
public class CSATEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CSATID { get; set; }
// foreign keys
public int ClientID { get; set; }
public int ContactID { get; set; }
// nav props
[ForeignKey("ClientID")]
public virtual CompanyEntity CompanyEntity { get; set; }
[ForeignKey("EmployeeID")]
public virtual EmployeeEntity EmployeeEntity { get; set; }
... more props
The exception doesn't note which model is jacked up, or if all of them are. What is the best way to hunt this down?
回答1:
Try changing namespace from
System.Data.Objects.ObjectContext to System.Data.Entity.Core.Objects.ObjectContext
System.Data.Objects to System.Data.Entity.Core.Objects
Take a look at this MSDN page http://msdn.microsoft.com/en-us/data/dn469466. It explains how to upgrade to Entity Framework 6
来源:https://stackoverflow.com/questions/21787156/ef5-to-ef6-upgrade-navigation-properties-are-broken