最佳实践在EF代码优先查找表 [英] Best Practices for Lookup Tables in EF Code-First
问题描述
我在做我的第一个EF项目,我打算去代码优先模式。我试图找到一些关于处理一个相当经典的查找表的情景指导。
我处理一个非常典型的情况,我会坚持地址数据。所以,我有一个简单的地址DTO ...
公共类地址
{
公众诠释标识{搞定;组; }
公共虚拟字符串StreetAddress1 {搞定;组; }
公共虚拟字符串StreetAddress2 {搞定;组; }
公共虚拟字符串城{搞定;组; }
公共虚拟字符串国{搞定;组; }
公共虚拟字符串邮编{搞定;组; }
}
在国家财产,我想存储标准的两个美国-letter状态代码。为了验证目的,我想有所得的地址表和一个相当标准的状态查找表之间的一个标准一对多外键关系。该表中可能会包含一个ID,这两个字母的代码,第三列包含完整的国家名称。
我希望用这种状态查找表填充和状态下拉式的箱等,也可用作验证在地址实体提交的国家。相当普遍的东西。所以,我有几个简单的(我希望)的问题。
- 请我需要创建代表的国家实体,只是一个实体
具有EF创建表,或者我可以只包括在DBCreation策略表创建
流程和种子在那里? - 难道是有意义的创建实体,只是为视图
款任何地方,我想显示状态选取器 - 使用我真的只想存储两个字母的州代码在地址
的实体,但是,这是否有意义,或者它更有意义,只是
使它成为一个导航属性到一个国家实体,然后显示?
我挣扎了一下这里阐述我的观点,所以如果我不清楚,请随时要求更多的细节。
在此先感谢。
适当地在UI?
- 我会做它的状态,自己的阶级和地址的导航属性。
公共类地址
{
酒店的公共INT标识{搞定;组; }
公共虚拟字符串StreetAddress1 {搞定;组; }
公共虚拟字符串StreetAddress2 {搞定;组; }
公共虚拟字符串城{搞定;组; }
公共虚拟USState国家{搞定;组; }
公共虚拟字符串邮编{搞定;组; }
}
公共类USState
{
公众诠释标识{搞定;组; }
公共字符串代码{搞定;组; }
公共字符串文本{搞定;组; }
}
通过代码首先EF将创建表,但你可以填充它在种子()
方法。
- 您不一定需要使用视图车型,但它是有道理的使用共享视图为编辑表格显示的状态。你不提MVC,但如果您使用的,那么它的简单,只要把
[UIHint(StatePicker)]
公共虚拟USState国家{搞定;组; }
在你的POCO或视图模型 - 这取决于你的视图使用。然后在查看/共享/ EditorTemplates,添加一个局部视图StatePicker.cshtml,这将looke像
@inherits的System.Web .Mvc.WebViewPage< USState>
@ Html.DropDownListFor(M =>男,新的SelectList((IEnumerable的< USState>)ViewBag.USStatesAll,
ID,
名,
型= ?= NULL 1:Model.Id),
Choose--)
在以
组合
@ Html.EditorFor(M = GT; m.State)
在您的视图。
- 导航属性。您的数据库将USState ID存储为外键,但您的应用程序可以使用addr.State.Code或addr.State.Text,根据需要。它更加灵活。
- 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?
- 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"
- 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 would make the state it's own class and a navigation property of the Address.
- 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
- 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.
醇>
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?
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.
这篇关于最佳实践在EF代码优先查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!