Solved

.NET MVC 5 Code First from Existing Database Primary Key Problem

Posted on 2015-01-06
1
321 Views
Last Modified: 2015-01-06
Hi Experts,

I have a .NET MVC project, and an existing database, and I'm trying to generate a code-first model from it. Several of the tables work fine, and several do not. For example, one table called "Actions", with a primary Key "ActionID" (String) generates a model that has no primary key. I have another table, called "LogEntities" with primary key "EntityID" (String), and that one detects the key just fine. I know there are conventions when identifying the key, but it seems that the tables that follow the convention fail, and the ones that don't... work properly?

I can't change the structure of the database, as it's accessed by other applications, so how can I either:

1) Force the primary keys to be detected as primary keys
2) Force the model builder to use them as keys anyway

I've tried this in the model builder (which i got from a stack overflow post, translated to VB):

modelBuilder.Properties(Of String).Where(Function(x) x.Name = "ActionID").Configure(Function(x) x.IsKey)

Open in new window


Which supposedly will force any field called "ActionID" to be used as a primary key, but that has no effect. Please help!

Thanks!
0
Comment
Question by:mylescardiff
1 Comment
 
LVL 1

Accepted Solution

by:
mylescardiff earned 0 total points
ID: 40534555
Sorry, stumbled on the answer to this 5 min after posting:

For anyone else with this problem, you need to add the following lines for each table that does not have a key defined:

 modelBuilder.Entity(Of Action).HasKey(Function(x) x.ActionID)

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now