MVC 使用entity framework 访问数据库 发布IIS
2. MVC工程
3. EF 参考
工程架构:

对应实体类:
public class MvcUser
{
public int Id { get; set; }
public string Name { get; set; }
public string PassWord { get; set; }
public int UserType { get; set; }
}
public class UserConfigurature:EntityTypeConfiguration<MvcUser>
{
public UserConfigurature()
{
ToTable("UserInformation");
HasKey(x => x.Id);
}
}
public class EFDbContext:DbContext
{
public DbSet<MvcUser> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserConfigurature());
base.OnModelCreating(modelBuilder);
}
}
测试代码:(增删改都测试过)延迟加载
/// <summary>
/// 插入测试
/// </summary>
[TestMethod]
public void InsertTest()
{
context.Users.Add(new MvcUser()
{
Name = "EF测试",
PassWord = "wwwwww",
UserType =
});
context.SaveChanges();
} [TestMethod]
public void ModifyTest()
{
MvcUser user = context.Users.Find();
user.Name = "修改后";
context.SaveChanges();
} [TestMethod]
public void DeleteTest()
{
context.Users.Remove(context.Users.FirstOrDefault(x=>x.Id == ));
context.SaveChanges();
}
测试工程的配置文件<configuration>节点添加:
<connectionStrings>
<!--Data Source=(LocalDb)\MSSQLLocalDB;-->
<add name="EFDbContext"
connectionString="
Server=IP 地址;
Initial Catalog=数据库名; User ID=用户名;
Password=密码;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
- integrated security=true 的意思是集成验证,也就是说使用Windows验证的方式去连接到数据库服务器。
MVC 使用entity framework 访问数据库 发布IIS的更多相关文章
- ASP.NET MVC+Entity Framework 访问数据库
Entity Framework 4.1支持代码优先(code first)编程模式:即可以先创建模型类,然后通过配置在EF4.1下动态生成数据库. 下面演示两种情形: 1.代码优先模式下,asp.n ...
- 使用MiniProfiler给Asp.net MVC和Entity Framework号脉(附源码)
在学习python开发框架pylons/pyramid的过程中,里面有个非常棒的页面性能监控功能,这样在开发过程中,你能清楚的知道当前页面的性能以及其它参数. 这里介绍一下如何给Asp.net MVC ...
- Mvc5+Entity Framework6 之二----在MVC中用Entity Framework实现基本的CRUD
目标:创建控制器和视图的代码,实现CRUD(创建,读取,更新,删除)功能 创建一个详细信息页 控制器为Students的Index页生成的代码排除Enrollments属性在外,因为该属性中关联着一个 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第六章:管理产品图片——多对多关系(上篇)
在这章中,我们将学习如何创建一个管理图片的新实体,如何使用HTML表单上传图片文件,并使用多对多关系将它们和产品关联起来,如何将图片存储在文件系统中.在这章中,我们还会学习更加复杂的异常处理,如何向模 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之目录导航
ASP.NET MVC with Entity Framework and CSS是2016年出版的一本比较新的.关于ASP.NET MVC.EF以及CSS技术的图书,我将尝试着翻译本书以供日后查阅. ...
- ASP.NET MVC+EasyUI+Entity FrameWork 整合开发
本文详细讲解怎么用ASP.NET MVC+EasyUI+Entity FrameWork 来开发一个项目 对于ASP.NET MVC的Jscript库,主要引用 <script type=.mi ...
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...
随机推荐
- POJ 2407 (欧拉函数)
题目链接: http://poj.org/problem?id=2407 题目大意:求小于n且与n互质的正整数个数. 解题思路: 欧拉函数=小于n且与n互质的正整数个数. 公式=n*(1-1/P1)* ...
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array
注意p的边界情况,p为0,或者 p为k 奇数+偶数 = 奇数 奇数+奇数 = 偶数 #include <iostream> #include <vector> #include ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- xcode 编译opencv ios容易出现的错误
(1)出现 "std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()"之类 ...
- java开发_模仿百度文库_OpenOffice2PDF_注意事项
在模仿百度文库的操作过程中,有很多朋友反映出来的一些问题,是我想起了写这篇blog. 主要是让大家在做的过程中注意一些东西,否则达不到想要的效果. 第一步:我们先从 java开发_模仿百度文库_Ope ...
- HDU 2955(0-1背包问题)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/M 题目: Description The aspir ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- [CareerCup] 17.14 Unconcatenate Words 断词
17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...
- Linux_几个符号命令
一.管道符号 | (将前面命令的输出结果传给后面的命令) [eRrsr@hadoop09-linux ~]$ cat /etc/passwd | grep "^root" root ...