• 一个linq to sql 问题


    Model代码

    //唱片表

    [Table(Name="Album")]

    public class Album

    {

    //外键引用

    private EntityRef<Genre> _Genre;

    private EntityRef<Artist> _Artist;

     

    //构造方法

    public Album()

    {

    this._Artist = default(EntityRef<Artist>);

    this._Genre = default(EntityRef<Genre>);

    }

     

    [Column(IsPrimaryKey = true, Name = "AlbumId", DbType = "Int NOT NULL IDENTITY")]

    public int AlbumId { get; set; }

    [Column(Name="GenreId")]

    public int GenreId { get; set; }

    [Column(Name = "ArtistId")]

    public int ArtistId { get; set; }

    [Column(Name = "Title")]

    public string Title { get; set; }

    [Column(Name="Price")]

    public float Price { get; set; }

    [Column(Name = "AlbumArtUrl")]

    public string AlbumArtUrl { get; set; }

     

    [Association(Storage = "_Artist", ThisKey = "ArtistId")]

    public Artist Artist

    {

    get { return this._Artist.Entity; }

    set { this._Artist.Entity = value; }

    }

    [Association(Storage="_Genre",ThisKey="GenreId")]

    public Genre Genre

    {

    get { return this._Genre.Entity; }

    set { this._Genre.Entity = value; }

    }

    }

     

    //歌曲风格(流派)

    [Table(Name="Genre")]

    public class Genre

    {

    private EntitySet<Album> _Albums;

     

    public Genre()

    {

    this._Albums = new EntitySet<Album>();

    }

     

    [Column(IsPrimaryKey=true,Name="GenreId")]

    public int GenreId { get; set; }

    [Column(Name="Name")]

    public string Name { get; set; }

    [Column(Name = "Description")]

    public string Description { get; set; }

     

    [Association(Storage = "_Albums", ThisKey = "GenreId")]

    public EntitySet<Album> Albums

    {

    get { return this._Albums; }

    set { this._Albums.Assign(value); }

    }

    }

     

    //艺术家

    [Table(Name="Artist")]

    public class Artist

    {

    private EntitySet<Album> _Albums;

     

    public Artist()

    {

    this._Albums = new EntitySet<Album>();

    }

    [Column(IsPrimaryKey=true,Name="ArtistId")]

    public int ArtistId { get; set; }

     

    [Column]

    public string Name { get; set; }

     

    [Association(Storage = "_Albums", ThisKey = "ArtistId")]

    public EntitySet<Album> Albums

    {

    get { return this._Albums; }

    set { this._Albums.Assign(value); }

    }

    }

     

    //音乐商店

    public class MusicStoreModels:DataContext

    {

    public Table<Album> Albums;

    public Table<Genre> Genres;

    //默认构造方法

    public MusicStoreModels(string connection):base(connection){}

    }

    音乐风格Controller

    [HandleError]

    public class HomeController : Controller

    {

     

    public ActionResult Index()

    {

    //获得连接字符串

    string connStr = ConfigurationManager.ConnectionStrings["MusicStoreContext"].ConnectionString;

    //

    MusicStoreModels db = new MusicStoreModels(connStr);

     

    //获取10个流派

    var viewModel =

    (from g in db.Genres

    select g).Take(10);

     

    ViewData["Genres"] = viewModel;

    return View();

    }

    }

    音乐风格展示View

    <h2>音乐风格</h2>

    <ul>

    <%foreach (var gener in (IEnumerable<Genre>)ViewData["Genres"])

    {

    %><li><%=Html.ActionLink(gener.Name,"Borwse","Store",new{gener=gener.GenreId},new{Title=gener.Description}) %></li><%

    } %>

    </ul>

    唱片Controller

    public class StoreController : Controller

    {

    //

    // GET: /Store/

    // 某类音乐风格下的唱片

    public ActionResult Borwse()

    {

    //获取查询音乐分类

    int gener = Int32.Parse(Server.HtmlEncode(Request.QueryString["gener"]));

     

    //获得连接字符串

    string connStr = ConfigurationManager.ConnectionStrings["MusicStoreContext"].ConnectionString;

     

    MusicStoreModels db = new MusicStoreModels(connStr);

    string generName =

    (from g in db.Genres

    where g.GenreId == gener

    select g.Name).First();

     

    //查询某类音乐风格的唱片

    var albums =

    from a in db.Albums

    where a.GenreId == gener

    select a;

     

    ViewData["generName"] = generName;

    ViewData["album"] = albums;

    return View();

    }

    }

    唱片View

    下面的foreach里面的转换就提示: 转换无效

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

    <%@ Import Namespace="MvcApplication.Models" %>

    <h2>

    音乐分类:<%=ViewData["generName"]%></h2>

    <ul>

    <%foreach (var album in (IEnumerable<Album>)ViewData["album"])

    {%>

    <li><%=album.Title %></li>

    <%

    } %></ul>

    有点想不通,一样的代码.一样的类型.怎么就转换无效呢.

  • 相关阅读:
    Ubuntu 16.04配置vncviewer
    Ubuntu中可以卸载的软件(持续更新)
    MySQL入门常用命令
    数据库学习笔记(一)
    TensorFlow学习笔记(一)
    ubuntu安装deb包(dpkg)
    Linux中的bin文件夹
    常对象成员和常成员函数
    this指针
    对象成员指针
  • 原文地址:https://www.cnblogs.com/Dtscal/p/2549111.html
Copyright © 2020-2023  润新知