asp.net MVC + linq to Entity简单教程(一)MVC的建立与Entity的建立

  最近在写一个项目,是一个用于手机浏览器浏览的一个校园办公的系统,主要是采用HTML5 + asp.net MVC进行编写的,平时在写程序的时候,很少用到MVC,一般都是用三层架构,因为MVC架构,生成的前台代码,很干净,没有一些乱七八遭的代码,很适合与HTML5结合。在开发的时候,我们采用的model模型,依然是我们最熟悉是的ado.net。程序在测试完成,交付使用之后,自己感觉可以借这个机会,好好对MVC进行学习和总结一下,所以就看了一些文章,发现一般在使用MVC的时候,数据模型一般都采用 Entity framework。自己也就一起学了一下。现在记下自己学习过程中的一些总结,问题。与大家共同分享,有不对的地方,希望大家给与指正。

  我的开发环境是 VS2008 + sqlserver 2005   +  MVC3.5

        好了,我们开了,这里说一点,因为这个程序我已经开发了一半了,所以有些前期的东西,就是是另外起的一个名字,建立的新工程。

  一,建立一个MVC工程。这个不多说了,我想大家都会建立的,说多了,也就没有意思了。

    

(图1)

       二,在生成好的MVC工程里,我们在Models文件夹里建立我们的 Entity模型

    

(图2)

    

     (图3)

    这里我们选择第一项

    

(图4)


    在下面图中,你在新建的时候,点new Connection进行连接数据库,说一下,下面有一个 NO,YES的选项,它的意思是你是否把连接数据库的密码写到web.config文件中,这个根据你自己的喜欢和习惯而定了

    

(图5)

               点击 “New Connection” 会出现下面的样子,不多说了,这个很简单,

    

(图6)

    点“OK”回到上一个图,点next,就是会出现,让我们让我们选择表,视图,存储过程的页面了

    

(图7)

    选择你要在这个项目中运用到的表,视频或是存储过程了,当然了,你也可以在以后的程序编写过程中,慢慢加。选择之后,点“Finish”,这时候在工程里,就会生成一个叫test.edmx的文件

    

(图8)

    

(图9)


    这样,一个Entity数据模型就建立完成了。它会为你自动生成,表之间的关系,表的结构。自动你建立起,添加,修改,删除的功能。(注:我只找到删除和添加)

    生成完之后,你就可以向调用一个类一样,在controls文件中,实例化一个,来进行操作了。名字是什么啊?哈哈。。。在图5中的最下面,有一个“Save Entity......”的文本框,这个就是它的名字了,你只要实例化一下就行了,如下

    

NewETrainEntities newEtrain = new NewETrainEntities();

    说明一下,Entity有自己的操作方法,我没有采用,我用的是linq to Entity.这一点一定要注意

    这里说几个我遇到的问题,在图9中,我们可以看到,我们选择的表,视图,都会在这个页面中显示出来,如果我们不小心,选错了一个表,并不想用它,怎么办呢。我们第一个选择就是去图9中,把这个表删除了,可是你会发现,当你再想建这个表时候,你会发现,你根本就找不到这个表,因为它显示你已经建完了。这是为什么呢。我已经删除了,为什么还在呢。因为你删除只是一个视图,它的还是存在于这个模型之中的,大家看下图

    

    (图10)

  在MODEL BROWER中我们可以看清楚看到,你删除的在NewEtrainModel1.Store里没有,可是在上面的NewEtrainModel中还是存在的。

  一般清楚下,我们会认为,我删除了,再加回来呗,可是我在这里,是死活也没有找到可以加回来的方法,如果有,请朋友告诉我。

怎么呢,我只能用Editplus打开了这个Model1.edmx文件,在这里你会找到,你刚才删除的这个表的信息,你查询一下,把所有相关的信息全删除,然后,再去视图里,重建一下,就行了。

  我感觉这个是最笨的方法,VS不会不给提供找回的方法,只是我没有找到而已。


  这里提一点,因为linq to sql已经不更新了,说是现在主推是Entity。而且很多人还在讨论到底是Entity快呢。还是ado.net快呢。个人感觉应该是ado.net快啊。因为它是直接写SQL语句啊。而linq to Entity还需求再转一次,当然可能是我没有理解里面的机制吧

  这一章就介绍这些,下一章,我们将进行程序的开发和一些简单的linq to Entity的语法说明。

  对于一些基础的操作与基础知识,我在这里就不做介绍和说明了,网上有很多。


ASP.NET MVC with Entity Framework and CSS by Lee Naylor 2016 | ISBN: 1484221362 | English | 608 pages | True PDF | 30 MB This book will teach readers how to build and deploy a fully working example retail website using Microsoft ASP.NET MVC and Entity Framework technologies and recommendations. This book contains several solutions to real world issues that developers will frequently encounter. Whether you are a novice developer or an experienced .NET developer wishing to learn more about MVC and Entity Framework, author Lee Naylor will teach you how to develop a detailed database driven example website using Microsoft ASP.NET and Entity Framework Code First with fully explained code examples including: • Filtering and Searching Related Data using Linq • Using code first migrations to automatically update the database plus seeding data from code • How to use Visual Studio Scaffolding views and controllers • Manage CRUD operations including model binding as recommended by Microsoft and dealing with cascading deletions correctly • Input and data validation using Attributes • Sorting and paging through data and routing for friendly URL formats • Authorization and Authentication using Microsoft Identity v2 by making use of Microsoft example code including correct use of redirection after login and registration • Advanced techniques including managing data concurrency conflicts and using two database contexts • Asynchronous database access and paging • Azure deployment using Code First Migrations, remote debugging and viewing/editing your remote data • CSS Styling including animation and media queries What You Will Learn: • Get up and running quickly with ASP.NET MVC and Entity Framework building a complex web site to display and manage several related entities • How to integrate Identity code into a project • Advanced topics including: Asynchronous database access and managing data conflicts • Working with Microsoft Azure including remote debugging and database access • CSS skills including animations and media queries for use with tablet or mobile/cell phone devices Who This Book Is For: People wanting to learn ASP.NET MVC and Entity Framework ranging from novice developers new to the subject through to more experienced ASP.NET web forms developers looking to migrate from web forms to MVC and Entity Framework. The book assumes some programming knowledge such as object-oriented programming concepts and a basic knowledge of C#.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值