Asp.net mvc4如何使用linq连接实体
我在使用linq语言连接两个实体时遇到问题 我有模型类别:Asp.net mvc4如何使用linq连接实体,asp.net,linq,entity-framework,Asp.net,Linq,Entity Framework,我在使用linq语言连接两个实体时遇到问题 我有模型类别: public class Category : DbContext { [Key, Column(Order = 0),DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CategoryId { get; set; } Key, Column(Order = 1)] public int ShopId { get; set; } pub
public class Category : DbContext
{
[Key, Column(Order = 0),DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CategoryId { get; set; }
Key, Column(Order = 1)]
public int ShopId { get; set; }
public string Name {get;set;}
public virtual ICollection<Parameter> Parameter { get; set; }
}
这个关系是一对多的,所以一个类别可以有0…n个参数
更新:
当然,这种关系是多对多的。这就是为什么参数在模型中没有CategoryId属性的原因
我使用代码优先和迁移工具更新数据库
在数据库MSSQL Express中有3个表。类别、类别参数和参数。
表CategoryParameter是自动创建的,我没有该表的模型
使用多个参数创建新类别效果良好。所有3tables都包含有效数据
所以我现在的问题是:
我正在尝试加载一个类别的所有参数。该命令如下所示:
var parameters = from c in db.Categories
join p in db.Parameters
on new { ??? , c.ShopId } equals new { ??? , p.ShopId }
where c.ShopId == userProfile.ShopId && c.CategoryId == id
select new { ParamId = p.ParamId, Name = p.Name };
所以我的问题是,如果类中没有可用的atribute,如何连接这些表
非常感谢您的帮助 您不需要
加入
:改用导航属性:
参数属性
您不需要
加入
:改用导航属性:
参数属性
谢谢你的回复。我不知道为什么,但错误是“不能隐式将bool转换为int”,我必须确定,该类别属于ShopId。这就是为什么我在两种模式中都使用带有CategoryId/ParameterId的ShopId作为复合键的原因。eh,
=
而不是=
。。。更新了我的答案。我接受VB.NET C#切换…谢谢你的回复。我不知道为什么,但错误是“不能隐式将bool转换为int”,我必须确定,该类别属于ShopId。这就是为什么我在两种模式中都使用带有CategoryId/ParameterId的ShopId作为复合键的原因。eh,=
而不是=
。。。更新了我的答案。我正在忍受VB.NET C#切换。。。
var parameters = from c in db.Categories
join p in db.Parameters
on new { ??? , c.ShopId } equals new { ??? , p.ShopId }
where c.ShopId == userProfile.ShopId && c.CategoryId == id
select new { ParamId = p.ParamId, Name = p.Name };
var parameters = db.Categories.First(x => x.CategoryId == 10).Parameter.ToList();