当前位置: 代码迷 >> ASP.NET >> MVC3的model对应数据库中的表解决方法
  详细解决方案

MVC3的model对应数据库中的表解决方法

热度:1966   发布时间:2013-02-25 00:00:00.0
MVC3的model对应数据库中的表
请教 数据库中 city 主键是id 类型char(10),
对应model类
public class City
{
  public string id{get;set;}
  public string name{get;set;}
}
另数据库中一新表company : id ,cityid, companyname

public class Company
{
  public int id{get;set;}
  public int cityid{get;set;}
  public string companyname{get;set;}
  
  public virual City City{get;set;}
}


这里cityid 想用原来city的 id,同时希望company 的model能访问到city的name.不知道用什么方法解决。求高手指导

------解决方案--------------------------------------------------------
public virual City City
{
get { return db.Cities.SingleOrDefault(x => x.id == cityid); }
set { cityid = value.id; }
}
  相关解决方案