我想从app.config文件中获取连接字符串.
connectionString = System.Configuration.ConfigurationSettings.AppSettings["DBEntities"];
但它不起作用.它是空的.
我无法访问,System.Configuration.ConfigurationManager因为它是.net 4.
如何从中获取我的连接字符串app.config?
谢谢
我正在使用EF,当我这样做时:
foreach (var reg in detail.Regs)
{
this.db.Regs.DeleteObject(reg);
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
Collection was modified; enumeration operation may not execute.
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么???
我使用MySQL .net连接器6.4.4.0和实体框架工作4.1并尝试创建最基本的代码优先实现.
public class myDB: DbContext
{
public DbSet<Vote> Votes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的模特
public class Vote
{
public Guid Id { get; set; }
public int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的家庭控制员
public class HomeController : Controller
{
myDB_db = new myDB();
public ActionResult Index()
{
var model = _db.Votes;
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
我的强类型视图(使用List脚手架)
@model IEnumerable<Namespace.Models.Vote>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Value)
</th>
<th></th> …Run Code Online (Sandbox Code Playgroud) 我正在使用System.Data.Objects.EntityFunctions.TruncateTime方法在我的查询中获取日期时间的日期部分:
if (searchOptions.Date.HasValue)
query = query.Where(c =>
EntityFunctions.TruncateTime(c.Date) == searchOptions.Date);
Run Code Online (Sandbox Code Playgroud)
这个方法(我相信同样适用于其他EntityFunctions方法)不能在LINQ to Entities之外执行.在单元测试中执行此代码(实际上是对象的LINQ)会导致NotSupportedException抛出:
System.NotSupportedException:此函数只能从LINQ to Entities调用.
我DbSets在我的测试中使用存根来存储虚假存储库.
那我应该如何对我的查询进行单元测试?
StringLength验证的默认ErrorMessage比我想要的要长很多:
字段{Name}必须是最大长度为{StringLength}的字符串.
我想将其普遍改为:
最大长度为{StringLength}.
我想避免为我声明的每个字符串冗余地指定ErrorMessage:
[StringLength(20, ErrorMessage="Maximum length is 20")]
public string OfficePhone { get; set; }
[StringLength(20, ErrorMessage="Maximum length is 20")]
public string CellPhone { get; set; }
Run Code Online (Sandbox Code Playgroud)
我很确定我记得有一种简单的方法来普遍改变ErrorMessage但却无法回想起它.
编辑:
为了澄清,我试图普遍更改默认的ErrorMessage,以便我可以输入:
[StringLength(20)]
public string OfficePhone { get; set; }
Run Code Online (Sandbox Code Playgroud)
并有错误消息说:
最大长度为20.
我遇到了一个我必须使用Union all的场景,我如何在LINQ中实现这样的实体?
我正在使用Entity Framework Code First.使用LINQ to Entity我想根据DateTime值获取记录.这是我目前的代码:
/// <summary>
/// A method to check and see if the Parsed Game already exists on the
/// database. If Yes, then True is returned, otherwise False is returned.
/// </summary>
/// <param name="context">The Db Context to use</param>
/// <param name="homeTeam">The Name of the Home Team for the Game.</param>
/// <param name="awayTeam">The Name of the Away Team for the Game.</param>
/// <param name="date">The Date of the Game</param>
/// <returns></returns>
public bool doesGameAlreadyExist(PContext context, String …Run Code Online (Sandbox Code Playgroud) 我写了一些关于实体框架的假设,然后是几个问题(所以请纠正我错在哪里).我正在尝试使用EF 4的POCO.
我的假设:
假设这是正确的,这是我的问题:
如果不将所有实体保留在同一个EF图表上,那么如何保持数据完整性,如果没有"客户","订单"就不能存在? 这仅仅是存储库的一个功能,只是为了验证完整性而加载数据,还是我们"尝试/捕获"数据库参照完整性错误?
你不会为每个实体创建一个EF图吗? 例如,我不希望客户的更改和产品的更改一起写入,因为它们彼此无关(将它们放在同一图表上会导致它们一起写入).或者EF图的范围是否包含存储在同一存储介质中的所有类似实体?
将这样的实体分开是一种规范,还是只有一个图表来保存所有实体?我会想到后者,但我的想法越来越好.
我正在为WinForms应用程序编写查询管理器,除其他外,它需要能够在用户输入查询时向用户提供实时搜索结果(想想Google的实时结果,尽管很明显在厚客户端环境而不是Web).由于结果需要在用户输入时开始到达,搜索将变得越来越具体,所以我希望能够在用户输入更具体的信息时仍然执行时取消查询(因为结果会无论如何,只是被丢弃).
如果这是普通的ADO.NET,我显然可以使用该DbCommand.Cancel函数并完成它,但我们使用EF4进行数据访问,并且似乎没有明显的方法来取消查询.另外,在Reflector中打开System.Data.Entity并查看EntityCommand.Cancel显示一个令人沮丧的空方法体,尽管文档声称调用它会将其传递给提供者命令的相应Cancel函数.
我已经考虑过简单地让现有的查询运行并启动一个新的上下文来执行新的搜索(一旦完成就处理现有的查询),但我不喜欢单个客户端有多个开放的想法当我只对最近的结果感兴趣时,运行并行查询的数据库连接.
所有这一切都让我相信,一旦将EF查询分派到数据库中就没有办法取消EF查询,但是我希望这里的某个人能够指出我忽略的东西.
TL/DR版本:是否可以取消当前正在执行的EF4查询?
我有这个sql语句
SELECT userID from users WHERE
(name='name1' AND username='username1') OR
(name='name2' AND username='username2') OR
(name='name3' AND username='username3') OR
..........
(name='nameN' AND username='usernameN')
Run Code Online (Sandbox Code Playgroud)
我可以使用LINQ在实体框架中实现此语句吗?