Mapping select stored procedures in entity framework

后端 未结 2 617
失恋的感觉
失恋的感觉 2021-02-09 23:21

My scenario I\'m using Visual Studio 2010 with Entity Framework 4.1 I have a legacy database with many tables and many stored procedures. I\'m writing an ASP.NET C# program usin

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-10 00:01

    Except that you can, sort of.

    Take your most basic select stored procedure (i.e., the one which is closest to "select * from mytable", and use it to define a view in your database. Have entity framework use this "myview" instead of "mytable". Then map your insert, update and delete stored procs for this view-based entity as you did originally for your table.

    Finally, use function imports for your more selective selects, and define them to return collections of your entity. So if you had something like a Person entity, and you had a stored proc called something like FetchPersonByAge(int), your entity would end up with a static method called something like "GetByAge(int)". You could then call it in code like this: var people33 = Person.getByAge(33);

    I have done this, and it worked quite well, allowing me to respect a legacy database's designers demand that all database access be through their stored procs, and no user code directly accessing tables. See Julie Lerman's article:

    http://msdn.microsoft.com/en-us/data/gg699321.aspx

    Dave

提交回复
热议问题