本文介绍了EF代码首先一对多关系 - 未知对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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实体运行代码时,我收到以下异常:
无效的对象名称'dbo.Table_TableColumns'

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
class

{

 

public
int ID { 得到 ; set ;}

publicint ID {get;set;}

 

public
string 名称{
得到 ;
set ;}

publicstring Name {get;set; }

 

public
string 数据库{
得到 ;
set ;}

publicstring Database {get;set; }

 

public
虚拟
ICollection < TableColumn >
TableColumns {
得到 ;
set ;}

publicvirtualICollection<TableColumn> TableColumns { get;set; }

}

 

public

TableColumn

{

 

public
int ID {
得到 ;
set ;}

publicint ID {get;set; }

 

public
string 名称{
得到 ;
set ;}

publicstring Name {get;set; }

 

public
虚拟
表格{
得到 ;
set ;}

publicvirtualTable Table {get;set; }

}

 

public

MISContext
DbContext

{

 

public
DbSet < TableColumn >
TableColumns {
得到 ;
set ;}

publicDbSet<TableColumn> TableColumns { get;set; }

 

public
DbSet < >
表格{
得到 ;
set ;}

publicDbSet<Table> Tables { get;set; }

 

 

protected
覆盖
void OnModelCreating(System.Data.Entity.ModelConfiguration。 ModelBuilder
modelBuilder)

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 =>
new

{

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 =>
new

{

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
=> tc.Table).WithMany(t => t.TableColumns);

  ;

推荐答案

问题在于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:


modelBuilder.Entity<TableColumn>().MapSingleType(col => new
      {
        COLUMN_ID = col.ID,
        COLUMN_NAME = col.Name,
        TABLE_ID = col.Table.ID
      }).ToTable("METADATA_DATA_WAREHOUSE_COLUMNS");


这篇关于EF代码首先一对多关系 - 未知对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:05