Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:30:37+00:00 2026-06-01T12:30:37+00:00

I’m doing my first project with EF and I’m planning to go the code-first

  • 0

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.

  1. Do I need to create an entity to represent the State entity just to
    have EF create the table, or can I just include the table creation
    process in a DBCreation strategy and seed it there?
  2. Would it make sense to create that entity, just to use as “view
    models” for any place where I want to display a “state-picker”
  3. I really only want to store the two-letter state code in the address
    entity, but does this make sense or does it make more sense to just
    make it a navigation property to a state entity and then display?

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:30:38+00:00Added an answer on June 1, 2026 at 12:30 pm
    1. I would make the state it’s own class and a navigation property of the Address.
    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.

    1. You don’t necessarily need to use view models, but it makes sense to use a shared view for displaying the states in an edit form. You don’t mention MVC, but if you use that, then it’s as simple as putting
    [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.

    1. Navigation property. Your db will store the USState id as a foreign key, but your app can use addr.State.Code or addr.State.Text, depending on the need. It’s much more flexible.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.