问题描述
Hi, 我首先开始调查EF Code,以便在SQL Server 2000数据库上运行数据仓库解决方案。 我使用的简单示例有一个实体'Table',它与另一个名为'TableColumn'的实体有一对多的关系 Ive started investigation EF Code first for a data warehousing solution that runs on a SQL Server 2000 database. The simple example Im using has an entity 'Table' which has a one to many relationship with another entity called 'TableColumn' 我看了很多例子,我想我实现了这个正确,但每当我对TableColumn实体运行代码时,我收到以下异常: Ive looked at numerous examples, and I think im implementing this correctly, but whenever I run code against the TableColumn entity I receive the following exception:Invalid object name 'dbo.Table_TableColumns' 提前感谢您对此的任何帮助。 以下完整源代码: public { public publicint ID {get;set;} public publicstring Name {get;set; } public publicstring Database {get;set; } public publicvirtualICollection<TableColumn> TableColumns { get;set; } } public { public publicint ID {get;set; } public publicstring Name {get;set; } public publicvirtualTable Table {get;set; } } public { public publicDbSet<TableColumn> TableColumns { get;set; } public publicDbSet<Table> Tables { get;set; } protected protectedoverridevoid OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { base 。OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder); modelBuilder.Entity< modelBuilder.Entity< TableColumn >()。HasKey(col => col.ID); TableColumn>().HasKey(col => col.ID); modelBuilder.Entity< modelBuilder.Entity< 表 >()。HasKey(tab => tab.ID); Table>().HasKey(tab => tab.ID); modelBuilder.Entity< modelBuilder.Entity< 表 >()。MapSingleType(table => { TABLE_ID = table.ID, TABLE_ID = table.ID, TABLE_NAME = table.Name, TABLE_NAME = table.Name, DATABASE = table.Database DATABASE = table.Database })。ToTable( }).ToTable( " METADATA_DATA_WAREHOUSE_TABLES" ); "METADATA_DATA_WAREHOUSE_TABLES"); modelBuilder.Entity< modelBuilder.Entity< TableColumn >()。MapSingleType(col => { COLUMN_ID = col.ID, COLUMN_ID = col.ID, COLUMN_NAME = col.Name COLUMN_NAME = col.Name })。ToTable( }).ToTable( " METADATA_DATA_WAREHOUSE_COLUMNS" ); "METADATA_DATA_WAREHOUSE_COLUMNS"); modelBuilder.Entity< modelBuilder.Entity< TableColumn >()。HasRequired< 表 >(tc   ; 问题在于Code First需要知道从TableColumn到Table的外键所在的位置。假设你的METADATA_DATA_WAREHOUSE_COLUMNS表中有一个TABLE_ID列,你可以按如下方式更新TableColumn的映射: The issue is that Code First needs to know where the foreign key from TableColumn to Table lives. Assuming you have a TABLE_ID column in your METADATA_DATA_WAREHOUSE_COLUMNS table you can update your mapping for TableColumn as follows: 这篇关于EF代码首先一对多关系 - 未知对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
无效的对象名称'dbo.Table_TableColumns'
class
表
int ID { 得到 ; set ;}
string 名称{
得到 ;
set ;}
string 数据库{
得到 ;
set ;}
虚拟
ICollection < TableColumn >
TableColumns { 得到 ;
set ;}
类
TableColumn
int ID {
得到 ;
set ;}
string 名称{
得到 ;
set ;}
虚拟
表 表格{
得到 ;
set ;}
类
MISContext :
DbContext
DbSet < TableColumn >
TableColumns { 得到 ;
set ;}
DbSet < 表 >
表格{ 得到 ;
set ;}
覆盖
void OnModelCreating(System.Data.Entity.ModelConfiguration。 ModelBuilder
modelBuilder)
new
new
=> tc.Table).WithMany(t => t.TableColumns); 推荐答案
modelBuilder.Entity<TableColumn>().MapSingleType(col => new
{
COLUMN_ID = col.ID,
COLUMN_NAME = col.Name,
TABLE_ID = col.Table.ID
}).ToTable("METADATA_DATA_WAREHOUSE_COLUMNS");