Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建控制器时出现错误消息,请定义此实体类型的键_C#_Sql_Asp.net Mvc 3_Visual Studio_Key - Fatal编程技术网

C# 创建控制器时出现错误消息,请定义此实体类型的键

C# 创建控制器时出现错误消息,请定义此实体类型的键,c#,sql,asp.net-mvc-3,visual-studio,key,C#,Sql,Asp.net Mvc 3,Visual Studio,Key,尝试为数据库添加控制器时,我收到以下错误消息: 我对VisualStudio、C#和ASP MVC.net非常陌生,一直在学习教程,但为了更好地满足我的作业要求,我稍微改变了属性 我的代码如下所示: using System; using System.Collections.Generic; using System.Data; using System.Web.Mvc; using System.Linq; using System.Web; using System.Data.Entit

尝试为数据库添加控制器时,我收到以下错误消息:

我对VisualStudio、C#和ASP MVC.net非常陌生,一直在学习教程,但为了更好地满足我的作业要求,我稍微改变了属性

我的代码如下所示:

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using MvcPractise.Models;

namespace MvcPractise.Models {

public class Payments {

    [Key]
    public String Email { get; set; }
    public String PaypalEmail { get; set; }
    public String CardHoldersName { get; set; }
    public String CardNumber { get; set; }
    public String ExpirationDate { get; set; }
    public String CardType { get; set; }


}

public class PaymentsContext : DbContext
{

    public DbSet<PaymentsContext> Payment { get; set; }

    public DbSet<Payments> Payments { get; set; }

}
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Web.Mvc;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.ComponentModel.DataAnnotations;
使用MVCPracse.Models;
命名空间mvcpracise.Models{
公费{
[关键]
公共字符串电子邮件{get;set;}
公共字符串PaypalEmail{get;set;}
公共字符串名称{get;set;}
公共字符串卡号{get;set;}
公共字符串过期日期{get;set;}
公共字符串CardType{get;set;}
}
公共类付款上下文:DbContext
{
公共数据库集支付{get;set;}
公共数据库集{get;set;}
}
}

如果有人能帮忙的话,我一直在这个网站上搜索,并尝试了许多可能的解决方案,我重新创建了数据库并重写了代码,重新启动了VS,添加了代码中可以看到的[Key]类型。

重命名
类付款
类付款

那么您的PaymentsContext应该是:

public class PaymentsContext: DbContext
{
   public DbSet<Payment> Payments { get; set; }
}
公共类付款上下文:DbContext
{
公共数据库集{get;set;}
}

类付款
重命名为
类付款

那么您的PaymentsContext应该是:

public class PaymentsContext: DbContext
{
   public DbSet<Payment> Payments { get; set; }
}
公共类付款上下文:DbContext
{
公共数据库集{get;set;}
}

我相信问题出在您的PaymentsContext类中。您定义了一个DbSet支付属性,我认为您不打算这么做。PaymentsContext类只是DbContext的一个派生类,不应该是其自身的属性。以下内容可以解决您的问题:

    public class PaymentsContext : DbContext
    {
            public DbSet<Payments> Payments { get; set; }
    }
公共类付款上下文:DbContext
{
公共数据库集{get;set;}
}

我相信问题出在您的PaymentsContext类中。您定义了一个DbSet支付属性,我认为您不打算这么做。PaymentsContext类只是DbContext的一个派生类,不应该是其自身的属性。以下内容可以解决您的问题:

    public class PaymentsContext : DbContext
    {
            public DbSet<Payments> Payments { get; set; }
    }
公共类付款上下文:DbContext
{
公共数据库集{get;set;}
}

非常感谢,它工作得非常好,我花了几个小时试图修复它。我爱你。非常感谢你,它工作得很好,我花了几个小时试图修复它。我爱你。