[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2015-01-06
1
Medium Priority
?
351 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
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.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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