Connecting Firebird to an ASP.net WebAPI Project

放肆的年华 提交于 2019-12-21 19:47:57

问题


I'm in the process of learning ASP.net, specifically WebAPI and MVC. I'm using Visual Studio Community 2013, .NET 4.5, and C#. I'm a total newb so I'm actually going through this particular walkthrough to understand how things work:

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

It's been ok so far, but I want to move on to connecting a database to populate my dataset. I'm very familiar with using Firebird and was able to install Firebird as a dataprovider (through NuGet and installing the appropriate DDEX files). Unfortunately, I'm having difficulty understanding how to query the database and populate my array.

Basically, this is what my code looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using FirebirdSql.Data.FirebirdClient;
using System.Configuration;
using System.Collections;

namespace NBAPoolWebService.Controllers
{
    public class UserController : ApiController
    {

        User[] users = new User[] 
        {
            new User { ID = 1, CREATED=new DateTime(2011, 1, 12), ISACTIVE='Y', USERNAME="TEST1"}, 
            new User { ID = 2, CREATED=new DateTime(2012, 2, 22), ISACTIVE='Y', USERNAME="TEST2"}

        };

        public IEnumerable<User> GetAllUsers()
        {
            return users;
        }

        public IHttpActionResult GetUser(int id)
        {
            var user = users.FirstOrDefault((p) => p.ID == id);
            if (user == null)
            {
                return NotFound();
            }
            return Ok(user);
        }
    }
}

Currently, if I visit http://localhost/api/user, I get my list of test users via JSON (which is what I want).

What I'm hoping to understand is how to change the array that I set to come from my Firebird database (via an SQL query). I know how to get my query (select * from users), I have a valid connection (confirmed through visual studio) and I have seen some tutorials on how to use FBConnection.

What I'm having difficulty with is actually using the data from the query and populate the objects in the array.

Any help would be greatly appreciated. If you could tell me the piece of code that I need to enter, or point me to the right tutorials, or if you think I should be using a specific technology or methodology, that would be great.

What I'm trying really trying to accomplish/understand is how to create a Web service that responds with JSON and that is connected to a Firebird database.

Thanks in advance for any help!


回答1:


I've learned a ton about .Net and Entity Framework by watching pluralsight videos. If you don't have an account with them, signing up for the trial would allow you to watch Entity Framework and Data Models. There is a module dedicated to Creating Database First Entity Data Models, which you may find useful.




回答2:


After a bunch of research, I stumbled upon the following link:

[link]http://johntomaselli.blogspot.ca/2012/04/mvc-4-firebird-ef-41-and-database-first.html

So I deleted my existing User Controller and Model and created a new Model using ADO.NET Entity Data Model (named User).

From there, I used EF Designed from Database and used the connectionstring I created in my web.config.

From there it let me choose the database tables I wanted and built out a Model called User.edmx.

After that, I created a controller using Web API2 Controller with action, using Entity Framework.

This allowed me to choose a model class with the corresponding name.

At this point, I think I need to do some research on the Entity Framework but this at least allows me to create a web service that connects to the database.

Hopefully this helps other young newbs on their start with ASP.net, MVC, WebAPI, Entity Frameworks, and Firebird.

My next steps will be understand the User.edmx and if I should have created a model for all tables (instead of just one). Hopefully I understand that when I resarch the Entity Framework.



来源:https://stackoverflow.com/questions/29370825/connecting-firebird-to-an-asp-net-webapi-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!