【问题标题】:Retrieve data from sql in MVC 4从 MVC 4 中的 sql 检索数据
【发布时间】:2015-03-26 17:50:28
【问题描述】:

平台:MVC4

嗨, 我是 MVC4 的新手,我正在尝试使用实体框架从 SQL 中检索数据,但它失败了。这是我到目前为止尝试过的代码:

模型:MovieListing.cs

[Table("MovieMaster")]
public class MoviesListing 
{
    public int MovieId { get; set; }
    public string Name { get; set; }
    public string year { get; set; }
    public string Image { get; set; }
    public string producer { get; set; }
    public string plot { get; set; }
}

型号:

public class MovieContext:DbContext
    {
        public DbSet<MoviesListing> Movies { get; set;  }
    }

查看:

@model MovieManiac.Models.MoviesListing

@{
    ViewBag.Title = "Moviedetails";
}

<h2>Moviedetails</h2>

<table>
    <tr>
        <td>Movie Name</td>
        <td> @Model.Moviename</td>
    </tr>


</table>

控制器:

public ActionResult MovieDetails(int id)
        {

                MovieContext objMovies = new MovieContext();
                MoviesListing movie = objMovies.Movies.Single(mov => mov.MovieId == id);

                return View(movie);


        }

所以,在浏览器中,当我输入 Id 时,我想从数据库中检索信息。我使用的是本地 sql db。有什么建议吗?

连接字符串:

</configSections>
  <connectionStrings>
    <add name="MovieContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MovieManiac-20150318225934;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MovieManiac-20150318225934.mdf" />
  </connectionStrings>

错误:

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'MoviesListing' has no key defined. Define the key for this EntityType.

\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Movies' is based on type 'MoviesListing' that has no keys defined.

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller


    【解决方案1】:

    您需要将 Key 属性添加到 Id 属性,以便 EF 知道主键是什么

    https://msdn.microsoft.com/en-us/data/jj591583.aspx

    http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

    更新

    您可以使用 int 和 numeric,尽管我不会打扰自己,只需将您的模型更改为 decimal 以匹配数据类型映射...https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx...如果您需要使用数字。

    也用 Key 属性装饰你的类

    [Table("MovieMaster")]
    public class MoviesListing 
    {
        [Key]
        public int MovieId { get; set; }
        public string Name { get; set; }
        public string year { get; set; }
        public string Image { get; set; }
        public string producer { get; set; }
        public string plot { get; set; }
    }
    

    您可以阅读有关使用小数(数字)作为键 http://dotnetwindow.blogspot.co.uk/2012/06/using-decimal-as-primary-key-in-entity.html

    顺便说一句,您是先使用代码吗?

    【讨论】:

    • 我无法为该属性添加密钥。只能加外键,我试过了,没用。
    • 如果它在数据库中是数字但在模型类中声明为 int,是否可以向字段添加键。
    【解决方案2】:
        add property to your entity 
        [Table("MovieMaster")]
        public class MoviesListing 
        {
            public int Id { get; set; }      // add this
            public int MovieId { get; set; }
            public string Name { get; set; }
            public string year { get; set; }
            public string Image { get; set; }
            public string producer { get; set; }
            public string plot { get; set; }
        }
    
    or else use this
    public class MovieContext:DbContext
        {
            public DbSet<MoviesListing> Movies { get; set;  }
    
     protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<MoviesListing>.HasKey(x => x.MovieId );
            base.OnModelCreating(modelBuilder);
        }
        }
    

    【讨论】:

    • 是的,但是我已经添加了主键 MovieId……我没有得到。
    • void 不支持覆盖。它抛出错误
    • 将 [Key] 属性添加到您的属性
    • 现在,添加键后出现不同的错误:System.Data.Entity.dll 中发生了“System.Data.EntityException”类型的异常,但未在用户代码中处理其他信息:底层提供程序在打开时失败。
    • 在 web.config 连接字符串中添加 providerName="System.Data.EntityClient"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多