基于实体框架的脚手架模型 - 验证错误

时间:2017-02-27 16:56:53

标签: c# asp.net-mvc entity-framework visual-studio

我的应用程序配置为使用SQLRoleManager。我只是创建了数据库,配置了web.config,让应用程序创建并填充数据库表。

我想要一个管理视图来执行一些简单的操作,例如添加和删除用户。因此,我使用脚手架向导创建了一个实体框架数据上下文,并指示它仅围绕表构建。 (没有检查视图和存储过程)。

我希望接下来要做的是然后将控制器搭建并查看并最终获得此数据的模型。当我右键单击控制器时,我选择使用实体框架添加带有视图的控制器。我填写适当的信息,为模型选择一个空类。

enter image description here

我收到以下错误,表示数据缺少密钥,但它们实际上是在数据库和实体模型中定义的。关于我在哪里出错的任何想法?我需要预先建立模型吗?我希望有视觉工作室自动创建这些。

enter image description here

修改

同样,这些表都有主键和外键。我可以从Sql Management Studio以及.edmx图中确认这一点。波纹管代码是自动生成的。我需要添加密钥吗?

Context.edmx

enter image description here

UserRole_Context.tt> aspnet_Users.cs

//------------------------------------------------------------------------------
// <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; }
}
}

UserRole_Context.Context.cs

//------------------------------------------------------------------------------
// <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=&quot;data source=*******;initial catalog=IDM;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

0 个答案:

没有答案