Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架外键到现有表列_Entity Framework_Entity Framework 6 - Fatal编程技术网

Entity framework 实体框架外键到现有表列

Entity framework 实体框架外键到现有表列,entity-framework,entity-framework-6,Entity Framework,Entity Framework 6,我的数据库中有下表 公司 资源ID(唯一标识符) 姓名(nvarchar(50)) 我的应用程序中有以下用户实体 public class ApplicationUser : IdentityUser { public String FirstName { get; set; } public String Surname { get; set; } public Guid CompanyId { get; set; } } 和一个配置类 public clas

我的数据库中有下表

公司

  • 资源ID(唯一标识符)
  • 姓名(nvarchar(50))
我的应用程序中有以下用户实体

public class ApplicationUser : IdentityUser
{

    public String FirstName { get; set; }
    public String Surname { get; set; }
    public Guid CompanyId { get; set; }

}
和一个配置类

public class ApplicationUserMap : EntityTypeConfiguration<ApplicationUser>
{

    public ApplicationUserMap()
    {



    }

}
公共类ApplicationUserMap:EntityTypeConfiguration
{
公共应用程序SERMAP()
{
}
}

使用fluentapi如何将应用程序用户中CompanyId的外键映射到现有表resourceid?

如果模型中不包含
公司
,则无法执行此操作

您只能在DB中使用标准T-SQL语句、DB初始化(
Seed()
)或自定义迁移(如果使用迁移)创建关系

该命令如下所示:

alter table ApplicationUser
add constraint App_Company_FK FOREIGN KEY (CompanyId) references Company(CompanyId)
要在
Seed
方法中运行它,请重写插入的
Seed()
方法,然后:

context.Database.ExecuteSqlCommand( ... );