1.对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性

在EF5.0修改实体的时候,出现“对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性这个错误

修改:

SaveChanges前先关闭验证实体有效性(ValidateOnSaveEnabled)这个开关

 db.Configuration.ValidateOnSaveEnabled = false;
int count = db.SaveChanges();
db.Configuration.ValidateOnSaveEnabled = true;

或者:

/// <summary>
/// 创建 EF上下文 对象,在线程中共享 一个 上下文对象
/// </summary>
/// <returns></returns>
public DbContext GetDbContext()
{
//从当前线程中 获取 EF上下文对象
DbContext dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext;
if (dbContext == null)
{
dbContext = new IZhanShiEntities();
dbContext.Configuration.ValidateOnSaveEnabled = false;
//将新创建的 ef上下文对象 存入线程
CallContext.SetData(typeof(DBContextFactory).Name, dbContext);
}
return dbContext;
}

2.EF中like怎么查询

See this discussion:

http://stackoverflow.com/questions/1033007/like-operator-in-entity-framework

The relevant part is towards the bottom:

Use SqlFunctions.PatIndex:

http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.patindex.aspx

Like this:

var q = EFContext.Products.Where(x =>
SqlFunctions.PatIndex("%CD%BLUE%", x.ProductName) > 0);

Note: this solution is for SQL-Server only, because it uses non-standard PATINDEX function.

I haven't tried it, but it looks promising.  You would say query = query.Where(w => w.Contacts.Any(c => c.Telephones.Any(p =>   SqlFunctions.PatIndex("%206%555%7777%") > 0  )));


http://blogs.msdn.com/b/eric_erhardt/

地址:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ede2db0-a4f9-4178-84c0-fabf4d3a57fa/how-to-generate-the-sql-like-linq-in-ls-query?forum=lightswitch

05-15 08:32