2

收到错误:

EntitySet 'User' 未在 EntityContainer 'festDBEntities' 中定义。接近简单标识符,第 1 行,第 46 列。

我的应用代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using FestCloud.festDBService;

namespace FestCloud
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Service1Client _serviceClient;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            _serviceClient = new Service1Client();
            _serviceClient.LoginUserCompleted += new EventHandler<LoginUserCompletedEventArgs>(_serviceClient_LoginUserCompleted);
        }

        void _serviceClient_LoginUserCompleted(object sender, LoginUserCompletedEventArgs e)
        {
            if ((e.Error == null) && (e.Result != null))
            {
                MessageBox.Show("Welcome " + e.Result + " !");
            }
            else
            {
                MessageBox.Show("Could not log in. Please check user name/password and try again.");
            }
        }

        private void logInButton_Click(object sender, RoutedEventArgs e)
        {
            _serviceClient.LoginUserAsync(userNameTextBox.Text, passwordTextBox.Text);
        }

        private void newAccountButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Register.xaml", UriKind.Relative));
        }
    }
}

和我的服务代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Objects;

namespace CloudServiceRole
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public void AddUser(string userName, string password)
        {
            using (var context = new festDBEntities())
            {
                context.AddToUsers(new User()
                {
                    UserName = userName,
                    Password = password,

                });
                context.SaveChanges();
            }
        }

        public string LoginUser(string username, string password)
        {
            string query = @"SELECT value User.UserName FROM festDBEntities.User AS User WHERE User.UserName = @username AND User.Password = @password";

            ObjectParameter[] parameters = new ObjectParameter[2];

            parameters[0] = new ObjectParameter("username", username);
            parameters[1] = new ObjectParameter("password", password);

            using (var context = new festDBEntities())
            {
                ObjectQuery<string> results = context.CreateQuery<string>(query, parameters);

                foreach (string result in results)
                {
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            return null; ;
        }
    }
}

我的 IService.cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace CloudServiceRole
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void AddUser(string userName, string password);

        [OperationContract]
        string LoginUser(string userName, string password);
        // TODO: Add your service operations here
    } 
}

我的数据库包含 UserId(int)-PK、UserName(nvarchar)、Password(nvarchar) 以及其他要在我掌握基础知识后添加的数据库。关于什么导致错误或我的代码的任何想法?非常感谢,MH

4

2 回答 2

0

这听起来不像是电话呼叫的问题,您是否尝试过从其他地方呼叫它?您是否尝试过调用不带参数的 Web 服务来查看是否有效?

您可能需要查看此示例以帮助您入门:

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/hosting-wcf-service-in-windows-azure-and-sumption-in-window-7-phone-app/

另外,我建议 WCF REST 并返回 JSON,例如:

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/how-to-consume-wcf-rest-service-returning-json-in-windows-ph/

于 2012-04-19T18:09:21.307 回答
0

这看起来像实体框架问题。最可能的原因是 festDBEntities 类不包含名为 User 的属性(也许它被命名为 Users?)。请先检查您的对象模型。根据我的经验,您还可以编写 LINQ 查询而不是对象查询。LINQ 查询是强类型的,可以帮助我们在编译时去除此类错误。

此外,还请在标签中添加实体框架,以便更多实体框架专家参与此线程以提供进一步的建议。

此致,

明旭。

于 2012-04-20T07:50:30.687 回答