简体   繁体   English

实体框架代码优先:如何为这种关系创建一对多?

[英]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.

相关问题 首先与实体框架代码建立多个一对多关系 - Multiple one-to-many relationships with entity framework code first 与代码优先实体框架的一对多关系 - One-to-many relationships with code-first Entity Framework 实体框架代码优先:如何在两个表之间创建一对多和一对一的关系? - Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables? 实体框架5代码优先中的一对一和一对多关系 - Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First 实体框架代码具有指向单个表的多个一对多关系的第一个配置 - Entity Framework Code First configuration with multiple one-to-many relationships pointing to a single table 实体框架Web.API中的代码优先一对多关系 - entity framework Code First One-to-Many relationships in Web.API 我可以在Entity Framework Code First中为复杂类型定义一对多关系吗? - Can I define a one-to-many relationship on a complex type in Entity Framework Code First? Entity Framework Core:一对多关系 - Entity Framework Core : one-to-many relationships 使用 MVC 3 Entity Framework Code-First,如何以一对多的关系创建一个新的“多”object? - Using MVC 3 Entity Framework Code-First, how do you create a new “many” object in a one-to-many relationship? 如何在实体框架中设置两个一对多关系? - How do I setup two one-to-many relationships in entity framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM