且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用EF直接访问数据库或通过使用EF的WCF访问?

更新时间:2023-12-02 23:40:22

你要找的东西是一个



真的没有理由使事情过于复杂通过使用WCF(不是为这种东西而设计的)。 Web API可与owin等一起使用,以处理用户帐户的身份。


I have an application that access to a Sql Server database. I have a repository in a dll that can be used by the client and can connect directly to the database.

Another option is that the client connect to a WCF services and this service use the entity framework repository to connect to the database.

As is expected, if the client use EF to connect directly to the database, is faster than doing through WCF, so I am wondering if this second option is a good idea or not.

The adavantanges of to use EF directly:

  • I don't have to maintenance the WCF layer.
  • It is faster.

Disavanteges:

  • Guess that I have many clients and I fix a bug in the repository, I have to ensure that all the clients are is updated to ensure that they use the last version to avoid problems in the data into the database. I don't know if with .NET I can control that a client can't access to database if a new versión of a dll is detected.

An option it would be to have only a copy of the client in a network folder, so the clients run this copy, but this is not a very good, because if I connect from a computer with a slow internet connection (with a VPN) then it is very slow because it has to load in memory all the application (at least the exe and other dlls) and this is very slow. So the solution is to run a local copy of the application, but then I have the same problem, how to ensure the client runs the last version of the repository.

If I use WCF:

  • I ensure that I use the last version of the repository.
  • Perhaps I can control better the authentication, although by the moment I don't need this feature.
  • In the future, I could develop a mobile application to access to the database. At least now, because windows phone applications can use directly entity framework and I need to connect through WCF. But if I am not wrong, with EF7 universal applications will be able to use EF too. Perhaps it would be useful for android applications.

Disadvantages:

  • I have to maintenace the WCF layer.
  • It is slow, because it has to serialize and deserialize and I notice a diference in speed.

So my question is, which is the common way or the general way to connect the database. I always connect in the LAN or if I am out, with a VPN created with OpenVPN, son in pracice, I always connect inside the LAN.

About security concerns, by the moment I dont control the authorization, so any user of the aplication can do it averything. In the future, I could control thorugh WCF or if I use EF directly, perhaps with permissions in the database, control the access to the tables.

Thanks so much.

The thing you're looking for is a Web Api. There are even web api 2 controllers that you can add, which are a template api controller with some example methods to be used together with Entity framework.

There's really no reason to make things overcomplicated by using WCF (which isn't designed for this kind of stuff). Web API can be used together with owin etc. to take care of the identities of the useraccounts.