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( ... );