I'm doing my first project with EF and I'm planning to go the code-first model. I'm trying to find a bit of guidance about handling a fairly classic "lookup table" scenario.
I'm dealing with a pretty canonical situation where I'll be persisting address data. So, I have a simple address DTO...
public class Address
{
public int Id { get; set; }
public virtual string StreetAddress1 { get; set; }
public virtual string StreetAddress2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string ZipCode { get; set; }
}
In the state property, I'd like to store the standard US two-letter state code. For validation purposes, I'd like to have a standard one-to-many foreign key relationship between the resulting Address table and a fairly standard state lookup table. That table would probably contain an ID, the two-letter code, and a third column to contain the full state name.
I would expect to use this state lookup table to populate and state drop-down style boxes, etc and also act as a validation to the State filed in the address entity. Fairly common stuff. So, I have a couple of simple (I hope) questions.
I struggled a bit with articulating my point here, so if I'm not clear, feel free to ask for more detail.
Thanks in advance. appropriately in the UI?
Creating Lookup Table Class Using Code-First Approch. Define Foreign Key Constraint Using Code-First Conventions. Disable Identity for Lookup Table IDs. Seeding Enum into Lookup Table.
Because table lookups and simple estimations can be faster than mathematical function evaluations, using lookup table blocks often result in speed gains when simulating a model.
public class Address
{
public int Id { get; set; }
public virtual string StreetAddress1 { get; set; }
public virtual string StreetAddress2 { get; set; }
public virtual string City { get; set; }
public virtual USState State { get; set; }
public virtual string ZipCode { get; set; }
}
public class USState
{
public int Id { get; set; }
public string Code { get; set; }
public string Text { get; set; }
}
With code first EF will create the table, but you can populate it in the Seed()
method.
[UIHint("StatePicker")]
public virtual USState State { get; set; }
in your POCO or view model - depending on what your view uses. Then in Views/Shared/EditorTemplates, add a partial view StatePicker.cshtml, which would looke something like
@inherits System.Web.Mvc.WebViewPage<USState>
@Html.DropDownListFor(m => m, new SelectList((IEnumerable<USState>)ViewBag.USStatesAll,
"Id",
"Name",
Model==null?1:Model.Id),
"Choose--")
in combination with
@Html.EditorFor(m => m.State)
in your view.
Yes you can just create a new DBCreation script extending the original script and create a state table which has no relation with the Entity Framework.
If I were you I would create the state entity. If state entity is not created, in the end you need to create it in the code, but populating this entity will be a problem, you will need to use sql (you may store this data in xml which seems a better option than storing in sql).
If you decide to store the table in the database and directly use it by creating an entity, making it a navigation property is a better option since you may use it directly while lazy loading or eager loading by including it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With