EF跨库查询,DataBaseFirst下的解决方案

出于各种原因,有时需要跨数据库访问某些数据表,有同学已经给出了解决方案,比如  http://blog.csdn.net/hanjun0612/article/details/50475800 已经解决了code first 下跨数据库访问。但是如果已经是通过数据库创建的模型用此方法。报错xxxxxxxx。经过摸索下面给出DataBase First 下的解决方案

一、创建同义词

本例中以查询银企互联系统中某用户代码表为例 BankDirectLinkEnterprise为  数据库名 CustromerCode 数据表名 BDE_CustomerCode为同义词名

1 CREATE  SYNONYM [dbo].[BDE_CustomerCode]  FOR  [BankDirectLinkEnterprise].[dbo].[CustomerCode]<br> --表结构如下

CREATE TABLE [dbo].[CustomerCode](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Code] [nvarchar](50) NULL,
[ReceivedCustomerID] [int] NULL,
[Name] [nvarchar](50) NULL,
[IsPrimary] [bit] NULL,
[IsChargeCode] [bit] NULL,
CONSTRAINT [PK_CustomerCode] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

 如果数据库不在一个服务器下,请参见 http://blog.csdn.net/fanbin168/article/details/51104990 文中解决

二、仔细审视项目中 xxxx.edmx文件

用xml文本编辑器打开xxxx.demx文件我们发现其结构大致可分为4部分

1.描述了数据库定义、2、描述了类定义、3、关联类与数据库、4、描述如果显示类图位置,具体见图(图中描述类与数据库定义写反了,见谅)

大概看懂之后就备份了一下,按需求改动

2、增加数据库中的表定义 在 <edmx:StorageModels> <Schema> 节点中加入以下代码

< EntityType  Name="BDE_CustomerCode">            < Key >              < PropertyRef  Name="ID" />            </ Key >            < Property  Name="ID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />            < Property  Name="Code" Type="nvarchar" MaxLength="100" />            < Property  Name="Name" Type="nvarchar" MaxLength="100" />            < Property  Name="ReceivedCustomerID" Type="int" />            < Property  Name="IsPrimary" Type="bit" />            < Property  Name="IsChargeCode" Type="bit" />          </ EntityType >

 增加容器定义  <edmx:StorageModels><Schema> <EntityContainer>节点下添加(虽然这个同义词在数据库中不存在,但是按其在原数据库中的类型来添加,一点问题没有)

1 < EntitySet  Name="BDE_CustomerCode" EntityType="Self.BDE_CustomerCode" Schema="dbo" store:Type="Tables" />

  

3、增加数据定义,   在 <edmx:ConceptualModels><Schema>节点中加入以下代码

1 2 3 4 5 6 7 8 9 10 11 < EntityType  Name="BDE_CustomerCode">           < Key >             < PropertyRef  Name="ID" />           </ Key >           < Property  Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />           < Property  Name="Code" Type="String" MaxLength="100" FixedLength="false" Unicode="true" />           < Property  Name="Name" Type="String" MaxLength="100" FixedLength="false" Unicode="true" />           < Property  Name="ReceivedCustomerID" Type="Int32"  />           < Property  Name="IsPrimary" Type="Boolean" />           < Property  Name="IsChargeCode" Type="Boolean" />         </ EntityType >

  增加容器定义  <edmx:ConceptualModels><Schema> <EntityContainer>节点下添加

1 < br >< EntitySet  Name="BDE_CustomerCodes" EntityType="DB_OnlineOrderModel.BDE_CustomerCode" />

    DB_OnlineOrderModel为此项目的model命名空间,大家按自己项目改掉

3、增加数据定义与数据库映射 <edmx:Mappings> Mapping节点下添加

1 2 3 4 5 6 7 8 9 10 11 12 < EntitySetMapping  Name="BDE_CustomerCodes">              < EntityTypeMapping  TypeName="DB_OnlineOrderModel.BDE_CustomerCode">                < MappingFragment  StoreEntitySet="BDE_CustomerCode">                  < ScalarProperty  Name="Code" ColumnName="Code" />                  < ScalarProperty  Name="ReceivedCustomerID" ColumnName="ReceivedCustomerID" />                  < ScalarProperty  Name="Name" ColumnName="Name" />                  < ScalarProperty  Name="IsPrimary" ColumnName="IsPrimary" />                  < ScalarProperty  Name="IsChargeCode" ColumnName="IsChargeCode" />                  < ScalarProperty  Name="ID" ColumnName="ID" />                </ MappingFragment >              </ EntityTypeMapping >            </ EntitySetMapping >

  

4找个位置显示它 <edmx:Designer>  <edmx:Diagrams>  <edmx:Diagram> 添加一条,如果不知道后面的pointx pointy 如何写,照葫芦画瓢后可调整

  <edmx:EntityTypeShape EntityType=”DB_OnlineOrderModel.BDE_CustomerCode” Width=”1.875″ PointX=”14.5″ PointY=”0.625″ />

5,关闭edmx文件,然后双击demx文件打开它,保存,这时vs一般会再按t4模版生成代码。完毕之后就可以调用了

6,使用

1 2 3 4 5 6 7 8 9 var  list =                  ( from  in  this .db.BDE_CustomerCodes                  where  p.Name.Contains(q) || p.Code.Contains(q)                  select  new                  {                      CompanyName = p.Name,                      CustomerCode = p.Code                  }).ToList();              return  Json(list, JsonRequestBehavior.AllowGet);

  

好了,方案完成了,但是此方案缺点还是很明显的,人工干预的过程太多了,哪一环节出了问题都会引发错误。要是能自动化支持同义词就更好了。