[英]Entity Framework Code First: How can I create a One-to-Many for this relationships?
so Iam new to code firt and I have a database that is done already and cant change and I get this error when I call clientes in my controller get this error because it look for a Empresa_id that not exist so how I can tell to look for the the correct field? 所以我是代码firt的新手,我有一个已经完成并且无法更改的数据库,当我在控制器中调用客户端时收到此错误,因为出现此错误是因为它查找不存在的Empresa_id,所以我可以告诉寻找正确的领域?
return View(clientes.ToPagedList(pageNumber, pageSize));
it return this select
SELECT
[Extent1].[id] AS [id],
[Extent1].[name] AS [name],
[Extent1].[empresa] AS [empresa],
[Extent1].[Empresa_id] AS [Empresa_id]
FROM [dbo].[cliente] AS [Extent1]
ORDER BY [Extent1].[name] ASC}
public class cliente
{
[Key]
public int id { get; set; }
public string name { get; set; }
public int empresa { get; set; } // foreing key
public virtual empresa Empresa { get; set; }
}
public class empresa
{
[Key]
public int id { get; set; }
public string descripcion { get; set; }
public virtual ICollection<cliente> Clientes { get; set; }
}
You must tell EF that empresa
is the foreign key for the navigation property Empresa
, otherwise it will assume a default foreign key name, that is "NavigationPropertyName+UnderScore+KeyNameInTargetClass" = Empresa_id
. 您必须告诉EF
empresa
是导航属性Empresa
的外键,否则它将采用默认的外键名称,即“ NavigationPropertyName + UnderScore + KeyNameInTargetClass” = Empresa_id
。 You can overwrite this default by applying the [ForeignKey]
attribute: 您可以通过应用
[ForeignKey]
属性覆盖此默认设置:
public class cliente
{
[Key]
public int id { get; set; }
public string name { get; set; }
[ForeignKey("Empresa")]
public int empresa { get; set; }
public virtual empresa Empresa { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.