Solved

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

Posted on 2015-01-06
1
323 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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.

776 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