在 MVC 4 中从 sql 检索数据

Retrieve data from sql in MVC 4

平台:MVC4

你好, 我是 MVC4 的新手,我正在尝试使用 Entity framework 从 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 时,我想从 database.I 检索信息,我正在使用本地 sql 数据库。有什么建议吗?

连接字符串:

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

您需要将 Key 属性添加到 I'd 属性 以便 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 属性装饰你的 class

[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

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