Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MVC4 Database-first, how to annotate models with a composite primary key?
    text
    copied!<p>I am new to MS's MVC4, and I have been given a database for which I must build a CRUD front-end. The tables all have composite primary keys of the form [TableID, TableName, EffectiveDate]. I cannot alter the database design. I used the Database-first technique and the <em>EF 5.x DbContext generator for C#</em> tool to create the models, but the generated model files do not contain annotations for the composite keys. Here is an example of the Department table, with primary key = [DeptID, DeptName, EffDate].</p> <pre><code>//------------------------------------------------------------------------------ // &lt;auto-generated&gt; // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // &lt;/auto-generated&gt; //------------------------------------------------------------------------------ namespace BillableUnits4.Models { using System; using System.Collections.Generic; public partial class Department { public int DeptID { get; set; } public string DeptName { get; set; } public System.DateTime EffDate { get; set; } public string Status { get; set; } public string Fund { get; set; } public string DeptNo { get; set; } public string RevenueAccount { get; set; } public string BalanceSheetAccount { get; set; } } } </code></pre> <p>I'm betting the keys should look like this:</p> <pre><code> [key] public int DeptID { get; set; } [key] public string DeptName { get; set; } [key] public System.DateTime EffDate { get; set; } </code></pre> <p>Do I even need to annotate the primary key components? If so, should I add the annotations to the generated model files? (regenerating the files would erase any changes I make by hand, obviously). Is there a way to tell MVC4 / Visual Studio to generate the files with the proper annotations? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload