使用实体框架连接到.NET MVC多个类似的表 [英] Using entity framework to connect to multiple similar tables in .net MVC

查看:78
本文介绍了使用实体框架连接到.NET MVC多个类似的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个相对较新的.NET MVC2和实体框架,我的工作,需要一个Web应用程序,一个项目(C#.NET 4),连接到接入不同的路线上有多个不同的数据库(即子)。

A relative newcomer to .net MVC2 and the entity framework, I am working on a project which requires a single web application, (C# .net 4), to connect to multiple different databases depending on the route of access, (ie subdomain).

原则上与此没有问题,所有的逻辑被写入到子域转变为一个实体连接,并且通过对实体模型通过这

No problem with this in principle and all the logic is written to transform the subdomain into an entity connection and pass this through to the Entity Model.

问题自带的事实,不同的数据库,同时在结构上大同小异含有3个或4个独特的表显出到该实例。

The problem comes with the fact that the different database whilst being largely similar in structure contain 3 or 4 unique tables bespoke to that instance.

在我看来,有两种办法来解决这个问题,无论是其中我相信将成为可能。

To my mind there are two ways to solve this issue, neither of which i am sure will be possible.

1 /使用一个独立的实体模型为每个数据库。结果
       -Attempts沿着这条路必须通过了冲突中的表/ SP名称是相同的跨越分贝不同的充的,或隐式转换错误,当我尝试把不同车型在不同的命名空间。搜索结果
    或

1/ Use a separate entity model for each database.
-Attempts down this route have through up conflicts where table/sp names are the same across differnt db's, or implicit conversion errors when I try and put the different models in different namespaces.

or

2 /覆盖这是指基于碱控制器属性的值的改变的数据库中的对象的类。结果
   - 我没有发现任何建议我甚至可以做到这一点。结果
  搜索结果

2/ Overwrite the classes which refer to the changeable database objects based on the value of a base controller property.
-I have found nothing to suggest i can even do this.


我的问题是,如果不是theser路线原则上可以不断的工作,或者我应该放弃对EF并连接到dtabases directlky使用ADO。
也许还有另一种方式来解决这个问题,我还没有想过呢?

My question is if either of theser routes can ever work in principle or if i should just give up on the EF and connect to the dtabases directlky using ADO. Perhaps there is another way to solve this problem i haven't thought of?

感谢您的帮助...

推荐答案

有趣的问题。

我建议你必须为每个数据库一个​​单独的电火花,并在模型上添加另一个层(类库)(一个DAL库)。这一层是从你的控制器调用的单点。然后在这一层,你可以直接到特定的数据上下文的调用并返回结果。您可能还希望建立共同的POCO对你的两个数据库,那么这与使用接口组合会给你一个强大的设计模式。

I'd recommend you have a seperate EDM for each database, and add another layer (class library) on top of model (a DAL repository). This layer would be a single point of call from your controller. Then in this layer you can direct a call to a particular data context and return the result. You might also like to create common POCO's for your two databases, then this combined with the use of interfaces will give you a strong design pattern.

这样,你的模型一无所知哪个数据库的连接 - 这是你的DAL库的工作

This way your model knows nothing about which database its connecting to - this is the job of your DAL Repository.

当您创建EDMX(在设计),请务必在属性窗口中指定命名空间是唯一的,你尝试使用数据上下文之前。

When you create your EDMX (in the designer), be sure to specify the "Namespace" in the properties window to be unique, before you attempt to use the data context.

当然,作为最后的手段(如果你上述不会工作),你可以为每个单独的电火花加工装配,并直接向特定的程序集的调用你的DAL作者

Of course as a last resort (if the above wont work for you), you could create seperate assemblies for each EDM, and direct a call to a specific assembly in your DAL.

下面就是我有我的设置(IM简单化,' - 蒙面方面)

Here's how i have mine setup (im simplistic, 'Foo'-masked terms)

我有一个名为 tblFoo 我的 EDMX

MyApp.Model.Repository (Class Library)
  - `Foo.edmx` (`internal`, namespace `MyApp.Model.Repository`)
  - `ILinqRepository.cs` (defines methods such as `IList<Foo> GetFoo`)
  - `EntityFrameworkRepository.cs` (concrete implemenation of `ILinqRepository`)
      projects a collection of `tblFoo` into type `Foo`
MyApp.Model.Service (Class Library)
  - `IDataService.cs` (similar signatures to `ILinqRepository`)
  - `LinqDataService.cs` (concrete implementation of `ILinqRepository`)
       'greedy' constructor accepts interface `ILinqRepository`
       and invokes methods on it
    ie:


public ILinqRepository _repository
public LinqDataService(ILinqRepository repository)
{
    _repository = repository;
}

public List<Foo> GetFoo()
{
    return _repository.GetFoo();
}
MyApp.Model.Entities (Class Library)
   - POCO classes such as "`Foo`".
MyApp.WebApplication (MVC2)

所以,我用 IOC (StructureMap)的接口的具体实现注入控制器/ ServiceModel。

So, i use IOC (StructureMap) to inject the concrete implementations of the interfaces into the Controllers/ServiceModel.

这方法是有1-1堆叠式的方法:

This way there is a 1-1 "stack-like" approach:

控制器 - > Model.Service - > Model.Repository - > EDMX

Controller -> Model.Service -> Model.Repository -> EDMX

在你的情况,你可以添加另一个 EDMX (说的表称为 tblFoo2 )。那么这两个你的方法将会从项目tblFoo - &GT;美孚 tblFoo2 - &GT;美孚

In your case, you could add another EDMX in your repository (say the table is called tblFoo2). Then both your repository methods would project from tblFoo -> Foo or tblFoo2 -> Foo.

您控制器有到POCO的,而不是表的引用(因为它们是内部)。

Your controllers have a reference to the POCO's, not the tables (as they are internal).

您可以在您的仓库,但不会触及任何其他层的轻松添加越来越多的功能。

You can easily add more and more functionality to your repository without touching any of the other layers.

现在我知道这可能是矫枉过正的情况下(你可能不喜欢使用IoC的 - 只是调用具体的类),但是这对我来说是什么在起作用。

Now i know this may be overkill for your scenario (you may not like to use IoC - just invoke on concrete classes), but this is what works for me.

希望这有助于!

这篇关于使用实体框架连接到.NET MVC多个类似的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆