Sometimes you need to refactor your database as your project progresses, and manually editing the Entity Framework classes may start to fail. In my case it sent me on a 2 day spiral into brain aneurysms that were - is it turns out - completely unnecessary.
The Errors I was getting?
System.InvalidOperationException: The ForeignKeyAttribute on property 'DSPID' on type 'SA.DS._0._2.Models.Assignments' is not valid. The navigation property 'DSPs_UniqueID' was not found on the dependent type 'SA.DS._0._2.Models.Assignments'. The Name value should be a valid navigation property name.How to Regenerate your Entity Framework Model classes quickly and correctly, without going to Redmond for 3 weeks to attend a $45,000 class.
First, create a NEW project. Do NOT attempt to do this inside an ongoing project.
Note that we picked Windows Desktop, Class Library.
Delete Class1.cs
Now right click the project (not the solution) and Add -> New Item…
Add -> Data -> ADO.NET Entity Data Model
Code First From Database
Choose Database, Save Connection, Change name to "Connection" instead of "Model", Next.
Import as many data objects as you want, or ALL of them.
Click Finish.
This will create all the table, view, and connection objects fresh. It also includes all key and foreign key data. You can ignore the warnings that it gives about views not having unique keys, that should really should be fixed in the database.
Now you can basically capture any items you need from the .cs classes that were just generated and either add them to, or cut/paste them into the existing project.
No comments:
Post a Comment