gpt4 book ai didi

c# - 添加 Controller 时无法检索模型的元数据

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:16 25 4
gpt4 key购买 nike

我作为新手正在“Visual Studio 2015 社区”上学习 ASP.NET MVC 5。我正在尝试添加一个带有 Entity Framework 模型的 Controller 。

enter image description here

然后出现错误。我很困惑。

enter image description here

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace TwiApp.Models
{
public class Twilio
{
[Key]
public int id { get; set; }
public string userId { get; set; }
public string sid { get; set; }
public string authToken { get; set; }
public string fromNumber { get; set; }
public string toNumber { get; set; }
public bool status { get; set; }
[ForeignKey("userId")]
public virtual ApplicationUser twi_appuser_ref { get; set; }
}
}

我到 SQL Server 2014 的连接字符串:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=TwiAppDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

最后,我的数据库上下文文件:

using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using TwiApp.Models;

namespace TwiApp.DAL
{
public class DatabaseContext : IdentityDbContext<ApplicationUser>
{
public DatabaseContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}

public DbSet<Twilio> Twilio_Db { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}

public static DatabaseContext Create()
{
return new DatabaseContext();
}
}
}

到目前为止我尝试了什么:

  1. ASP.NET MVC/EF Code First error: Unable to retrieve metadata for model
  2. Unable to retrieve metadata - MVC application
  3. Unable to Retrieve Metadata
  4. MVC4 Scaffolding Add Controller gives error "Unable to retrieve metadata..."
  5. Cannot create controller with Entity framework - Unable to retrieve metadata for ' '

任何答案将不胜感激。谢谢。

最佳答案

尝试按照以下方式更新您的 Twilio 类,EF 将计算出键和关系:

 public class Twilio
{
// [Key]
public int Id { get; set; }
public string sid { get; set; }
public string authToken { get; set; }
public string fromNumber { get; set; }
public string toNumber { get; set; }
public bool status { get; set; }
// [ForeignKey("userId")]
public string userId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}

关于c# - 添加 Controller 时无法检索模型的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com