C# 基于实体框架的脚手架模型-验证错误
我已将应用程序配置为使用SQLRoleManager。我只是创建了数据库,配置了web.config,让应用程序创建并填充数据库表 我想要一个管理视图来执行一些简单的操作,比如添加和删除用户。因此,我使用scaffolding向导创建了一个实体框架数据上下文,并指示它仅围绕表进行构建。未检查视图和存储过程 我希望做的下一件事是构建控制器和视图,并最终构建该数据的模型。当我右键单击控制器时,我选择使用实体框架添加带有视图的控制器。我为模型选择一个空类来填写适当的信息 我收到以下错误,表明数据缺少键,但实际上它们是在数据库和实体模型中定义的。你有没有想过我在这里哪里出了问题?我需要预先构建模型吗?我希望visual studio能够自动创建这些 编辑 同样,这些表都有主键和外键。我可以从SQLManagementStudio以及.edmx图中确认这一点。下面的代码是自动生成的。我需要添加密钥吗 Context.edmx UserRole\u Context.tt>aspnet\u Users.cs UserRole_Context.Context.cs 连接字符串C# 基于实体框架的脚手架模型-验证错误,c#,asp.net-mvc,entity-framework,visual-studio,C#,Asp.net Mvc,Entity Framework,Visual Studio,我已将应用程序配置为使用SQLRoleManager。我只是创建了数据库,配置了web.config,让应用程序创建并填充数据库表 我想要一个管理视图来执行一些简单的操作,比如添加和删除用户。因此,我使用scaffolding向导创建了一个实体框架数据上下文,并指示它仅围绕表进行构建。未检查视图和存储过程 我希望做的下一件事是构建控制器和视图,并最终构建该数据的模型。当我右键单击控制器时,我选择使用实体框架添加带有视图的控制器。我为模型选择一个空类来填写适当的信息 我收到以下错误,表明数据缺少键
你在告诉你错误:我没有钥匙,兄弟。为数据库提供密钥,并将其从堆积如山的数据中解放出来。如果要先编写代码,则需要设置一个字段,使类名与“classnameId”或“Id”匹配,或者在属性中添加一个“Key”属性。如果您首先使用数据库,请确保表具有主键。据我所知,EF不能处理堆,需要键控表。@djangojazz就是这样。我先做数据库。所有表都有一个PK,但我仍然会遇到这个错误。因为它说无法检索元数据,所以我猜测您正在尝试使用应用程序用户而不是admin/sa用户生成模型。但是,由于您还说您使用的是EDMX,您可能缺少与metadata=res://*Impl.Entities.SecurityModel.csdl | res://*Impl.Entities.SecurityModel.ssdl | res://*Impl.Entities.SecurityModel.msl类似的连接字符串部分,其中的名称与您的模型匹配。@Eris连接字符串为整个模型填充了元数据。它没有一个单独的表,这可能是问题吗?
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace IDM.DAL
{
using System;
using System.Collections.Generic;
public partial class aspnet_Users
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public aspnet_Users()
{
this.aspnet_PersonalizationPerUser = new HashSet<aspnet_PersonalizationPerUser>();
this.aspnet_Roles = new HashSet<aspnet_Roles>();
}
public System.Guid ApplicationId { get; set; }
public System.Guid UserId { get; set; }
public string UserName { get; set; }
public string LoweredUserName { get; set; }
public string MobileAlias { get; set; }
public bool IsAnonymous { get; set; }
public System.DateTime LastActivityDate { get; set; }
public virtual aspnet_Applications aspnet_Applications { get; set; }
public virtual aspnet_Membership aspnet_Membership { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<aspnet_PersonalizationPerUser> aspnet_PersonalizationPerUser { get; set; }
public virtual aspnet_Profile aspnet_Profile { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<aspnet_Roles> aspnet_Roles { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace IDM.DAL
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class IDMEntities : DbContext
{
public IDMEntities()
: base("name=IDMEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<aspnet_Applications> aspnet_Applications { get; set; }
public virtual DbSet<aspnet_Membership> aspnet_Membership { get; set; }
public virtual DbSet<aspnet_Paths> aspnet_Paths { get; set; }
public virtual DbSet<aspnet_PersonalizationAllUsers> aspnet_PersonalizationAllUsers { get; set; }
public virtual DbSet<aspnet_PersonalizationPerUser> aspnet_PersonalizationPerUser { get; set; }
public virtual DbSet<aspnet_Profile> aspnet_Profile { get; set; }
public virtual DbSet<aspnet_Roles> aspnet_Roles { get; set; }
public virtual DbSet<aspnet_SchemaVersions> aspnet_SchemaVersions { get; set; }
public virtual DbSet<aspnet_Users> aspnet_Users { get; set; }
public virtual DbSet<aspnet_WebEvent_Events> aspnet_WebEvent_Events { get; set; }
}
}
<add name="IDMEntities" connectionString="metadata=res://*/DAL.UserRole_Context.csdl|res://*/DAL.UserRole_Context.ssdl|res://*/DAL.UserRole_Context.msl;provider=System.Data.SqlClient;provider connection string="data source=*******;initial catalog=IDM;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>