现在的位置: 首页 > 综合 > 正文

在EF4.0中建立表与视图之间的关联(create association between TABLE and VIEW in EF4.0)

2012年07月06日 ⁄ 综合 ⁄ 共 1519字 ⁄ 字号 评论关闭
建立表和视图的脚本
create table Table1
(
    Id    
int identity(1,1not null,
    Name    
nvarchar(20not null,
    DepartmentId    
int not null,
    Attachment1    
image,
    Attachment2 
image,
constraint pk_Table1 primary key(Id)
)
go

create view Table1Simple
as
  
select id, Name, DepartmentCode, DepartmentName
    
from Table1 T
    
join sysDepartment D on T.DepartmentId=D.DepartmentId
go

将上面脚本建立的Table1表和Table1Simple视图添加到edm设计器中,然后添加一个Association,并将添加的Association关联到Table1Simple视图,问题出现了:

Error 3021: Problem in Mapping Fragment starting at line 72: Each of the following columns in table Table1Simple is mapped to multiple conceptual side properties: Table1Simple.id is mapped to <Table1Simple_Table1.Table1.id, Table1Simple_Table1.Table1Simple.id> 

 

恭喜你,这是EF4.0的一个BUG,而据官方说会在下一个发行版本中修正,值得高兴的是至少我们还有办法手工修正它:

用XML编辑器打开emdx文件,找到新建的Association,作如下修正:

<Association Name="Table1Simple_Table1">
  
<End Type="Cohl.Model.Table1" Role="Table1" Multiplicity="1" />
  
<End Type="Cohl.Model.Table1Simple" Role="Table1Simple" Multiplicity="1" />
</Association>

<!--我们添加一个引用约束如下所示:-->

<Association Name="Table1Simple_Table1">
  
<End Type="Cohl.Model.Table1" Role="Table1" Multiplicity="1" />
  
<End Type="Cohl.Model.Table1Simple" Role="Table1Simple" Multiplicity="1" />
  
<ReferentialConstraint>
    
<Principal Role="Table1"><PropertyRef Name="id"/></Principal>
    
<Dependent Role="Table1Simple"><PropertyRef Name="id"/></Dependent>
  
</ReferentialConstraint>
</Association>

 参考:http://blogs.msdn.com/b/adonet/archive/2008/12/05/table-splitting-mapping-multiple-entity-types-to-the-same-table.aspx

抱歉!评论已关闭.