【发布时间】:2015-04-28 16:00:26
【问题描述】:
我首先使用实体框架代码,我有三个表(例如):
public class Motorbike()
{
public int Id {get; set;}
public string Producent {get; set;}
public Engine Motor {get; set;}
public Tire Tires {get; set;}
}
public class Engine()
{
public int Id {get; set;}
public int Power {get; set;}
public decimal Price {get;set;}
}
public class Tire()
{
public int Id {get; set;}
public int Size {get; set;}
public decimal Price {get; set;}
}
这只是一个例子,实际上它更复杂。
Entity Frmaework 为我生成表,但表 Motorbike 有列:Id、Power、Engine_Id(仅存储数字 - id 引擎,而不是整个对象)和 Tire_Id(仅存储数字 - id轮胎,而不是整个物体)。
我知道如何插入数据 - 只需创建新的 Motorbike 对象,保存到他的字段数据(对于 Engine 和 Tire 字段,我不仅保存整个对象 id)并使用我的上下文中的 .Add() 方法。
但是如何获取摩托车 id 为(例如)1 的行的数据?
我尝试过这样的事情:
List<Motorbike> motorbikes= new List<Motorbike>();
var list = _context.Motorbike.Where(p => p.Id == 1);
motorbikes.AddRange(list);
但我总是为 Engine 和 Tire 字段设置 null(字段 Id 和 Producent 填写正确)。
【问题讨论】:
-
你能提供更多关于你的实体框架和数据库定义的信息吗?
标签: c# database linq entity-framework object