EF问题集合
1. 在使用数据迁移的过程中,如果手工删除了本地数据库之后,再次尝试连接被删除的数据库,会有以下提示:
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "ContosoUniversity3" requested by the login. The login failed.
修复方法:打开SQL Server Management Studio,连接上被删除数据库所在的引擎(一般是LocalDB),在Tool中再删除一次显示在列表中的数据库,会提示无法删除,文件不存在等等过,
不必管它,断开连接,再次连接,可以看到数据库已经不在列表当中,现在数据迁移可以再次使用被删除的数据库名
如果没有SSMS,可以执行‘sqllocaldb.exe stop v11.0’和‘sqllocaldb.exe delete v11.0’(未实测),以上知识来自下面的提问:
http://stackoverflow.com/questions/21592062/ef6-migrations-localdb-update-database-login-failed
http://stackoverflow.com/questions/13275054/ef5-cannot-attach-the-file-0-as-database-1/16339164#16339164
2. 使用数据迁移的流程:
来自:http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application
注释掉Web.Config当中的数据初始化器的代码
修改连接字符串,使用新数据库
执行enable-migrations
执行add-migration XXXXXX(为迁移生成快照,同时生成迁移用的文件,可以修改生成的文件)
执行update-database(更新改动到数据库当中)
3. 使用代码执行迁移,查看迁移生成的代码
https://romiller.com/2012/02/09/running-scripting-migrations-from-code/
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
var scriptor = new MigratorScriptingDecorator(migrator);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null); 4.EF在转换成JsonResult时遇到无限循环的解决办法
序列化类型为“System.Data.Entity.DynamicProxies.Photos_1F5D250F2735650E782711718DE2EFF2BBEA68EE8F6C5A1CF253FAABD0681F7B”的对象时检测到循环引用。
来自:http://www.cnblogs.com/gmxq/p/4921974.html
干净优雅的好方法
摘抄于下:
public class MyJsonResult : JsonResult
{
public JsonSerializerSettings Settings { get; private set; } public MyJsonResult()
{
Settings = new JsonSerializerSettings
{
//这句是解决问题的关键,也就是json.net官方给出的解决配置选项.
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
} public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("JSON GET is not allowed");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
if (this.ContentEncoding != null)
response.ContentEncoding = this.ContentEncoding;
if (this.Data == null)
return;
var scriptSerializer = JsonSerializer.Create(this.Settings);
using (var sw = new StringWriter())
{
scriptSerializer.Serialize(sw, this.Data);
response.Write(sw.ToString());
}
}
}
public class BaseController : Controller
{
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new MyJsonResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior };
} }
当成,自己组合匿名对象也是可以的,比如来自SO的一个代码片段:
http://stackoverflow.com/questions/16949520/circular-reference-detected-exception-while-serializing-object-to-json
The solution:
Return the data (properties) you need as anonymous objects.
A code example:
In this case I needed the 3 latest tickets, based on "Date Scheduled". But also needed several properties stored in related entities.
var tickets =
context.TicketDetails
.Where(t => t.DateScheduled >= DateTime.Now)
.OrderBy(t => t.DateScheduled)
.Take(3)
.Include(t => t.Ticket)
.Include(t => t.Ticket.Feature)
.Include(t => t.Ticket.Feature.Property)
.AsEnumerable()
.Select(
t =>
new {
ID = t.Ticket.ID,
Address = t.Ticket.Feature.Property.Address,
Subject = t.Ticket.Subject,
DateScheduled = String.Format("{0:MMMM dd, yyyy}", t.DateScheduled)
}
);
同样是在那篇帖子里面有回复AsNoTracking可以的,不过我测试的结果是不行,也许是6.1.3的版本和之前5.X的在这方面有区别?
规避json序列化的时候直接序列化该t_saleform对象,改为序列化其它没有这种映射关系的对象。代码如下:
1: public JsonResult GetSaleByNo(string id)
2: {
3: SaleMvcUI.Helper.saleDBEntities saleDB = new Helper.saleDBEntities();
4:
5: var saleF = (from sf in saleDB.t_saleform
6: where sf.f_saleform_no == id
7: select new
8: {
9: f_saleform_no = sf.f_saleform_no,
10: f_saleform_date = sf.f_saleform_date,
11: f_customer = sf.f_customer,
12: f_sales = sf.f_sales,
13: f_remark = sf.f_remark
14: }).First();
15: //此处为了好转换日期格式,多定义了一个临时变量。
16: var tm = new{
17: f_saleform_no = saleF.f_saleform_no,
18: f_saleform_date = saleF.f_saleform_date.ToString("yyyy-MM-dd"),
19: f_customer = saleF.f_customer,
20: f_sales = saleF.f_sales,
21: f_remark = saleF.f_remark
22: };
23: return this.Json(tm, JsonRequestBehavior.AllowGet);
24: }
EF问题集合的更多相关文章
- ASP.NET EF 延迟加载,导航属性延迟加载
ASP.NET EF 延迟加载,导航属性延迟加载 EF(EntityFramework)原理:属于ORM的一种实现 通过edmx文件来查看三部分:概念模型,数据模型,映射关系,上下文DbConte ...
- C#中集合接口关系笔记
IEnumerable IEnumerable接口是所有集合类型的祖宗接口,其作用相当于Object类型之于其它类型.如果某个类型实现了IEnumerable接口,就意味着它可以被迭代访问,也就可以称 ...
- (转载)哈夫曼编码(Huffman)
转载自:click here 1.哈夫曼编码的起源: 哈夫曼编码是 1952 年由 David A. Huffman 提出的一种无损数据压缩的编码算法.哈夫曼编码先统计出每种字母在字符串里出现的频率, ...
- Hive函数大全
一.关系运算: 1. 等值比较: = 语法:A=B 操作类型:所有基本类型 描述: 如果表达式A与表达式B相等,则为TRUE:否则为FALSE 举例: hive> select 1 from l ...
- Hive 字符串操作[转]
1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abcedfg') f ...
- MVC - 11(上).DTO
1.重要:javaScriptSerializer 无法识别被序列化的对象里各种属性是否存在 循环依赖 (System,Web.Script.Serialization.JavaScriptSeri ...
- hive支持sql大全
转自:http://www.aboutyun.com/thread-7316-1-1.html 一.关系运算:1. 等值比较: = 语法:A=B 操作类型:所有基本类型 描述: 如果表达式A与表达式B ...
- hive字符串函数
1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abcedfg') f ...
- hive支持sql大全(收藏版)
hive操作数据库还是比较方便的,因此才会有hbase与hive整合.下面我们hive的强大功能吧.为了增强阅读性,下面提几个问题: 1.hive支持哪些运算符? 2.hive是否支持左右连接? 3. ...
随机推荐
- vue中methods函数调用methods函数写法
export default { data() { return { hello:"你好" } }, methods:{ open(that) { that.hello = &qu ...
- Vue.js-----轻量高效的MVVM框架(七、表单控件绑定)
话不多说,先上完整代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- js学习笔记 -- await/ async
await 暂停async function函数,等待Promise处理完成,若Promise 状态为fulfilled,其回调resolve的参数作为await的值,Promise 状态为rejec ...
- hdfs namenode/datanode工作机制
一. namenode工作机制 1. 客户端上传文件时,namenode先检查有没有同名的文件,如果有,则直接返回错误信息.如果没有,则根据要上传文件的大小以及block的大小,算出需要分成几个blo ...
- 关于i++与++i的学习讨论!
先谈容易的知识点 区别两个 1. i++ 返回原来的值,++i 返回加1后的值. 2. i++ 不能作为左值,而++i 可以. 重点说下第二点.首先解释下什么是左值 左值是对应内存中有确定存储地址的对 ...
- 解决ajax提交中文数据乱码
function replaceWord(date,wordurl){ $.ajax({ url : 'wordReplace', ...
- /proc/sys/net/ipv4/下各参数含义
net.ipv4.tcp_tw_reuse = 0 表示开启重用.允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭 net.ipv4.tcp_tw_recycle = ...
- 性能测试工具LoadRunner21-LR之Controller 常用函数
1.事务函数: Lr_start_transaction(); //标记事务的开始 Lr_end_transaction(); //标记事务的结束,一般情况下,事务开始与结束联合使用 Lr_get ...
- 配置matcaffe 遇到的两个坑
1. 问题 (1) Invalid MEX-file '/root/caffe/matlab/+caffe/private/caffe_.mexa64': /matlab/r2016a/bin/gln ...
- Kudu的配置(官网推荐的步骤)(Configuring Apache Kudu)
不多说,直接上干货! http://kudu.apache.org/docs/configuration.html