[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2015-01-06
1
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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.

649 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