我在使用代码优先方法播种数据库时遇到此错误.
一个或多个实体的验证失败.有关详细信息,请参阅"EntityValidationErrors"属性.
说实话,我不知道如何检查验证错误的内容.Visual Studio向我显示它是一个包含8个对象的数组,因此有8个验证错误.
这与我之前的模型有关,但我做了一些修改,我在下面解释:
请原谅我的长代码,但我必须将其全部粘贴.在以下代码的最后一行中抛出异常.
namespace Data.Model
{
public class Position
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int PositionID { get; set; }
[Required(ErrorMessage = "Position name is required.")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
[Display(Name = "Position name")]
public string name { get; set; }
[Required(ErrorMessage = "Number of years is required")]
[Display(Name = "Number of years")]
public int yearsExperienceRequired { get; set; }
public virtual ICollection<ApplicantPosition> applicantPosition { get; set; …Run Code Online (Sandbox Code Playgroud) 我有这个查询,我在这个函数中得到错误:
var accounts = from account in context.Accounts
from guranteer in account.Gurantors
select new AccountsReport
{
CreditRegistryId = account.CreditRegistryId,
AccountNumber = account.AccountNo,
DateOpened = account.DateOpened,
};
return accounts.AsEnumerable()
.Select((account, index) => new AccountsReport()
{
RecordNumber = FormattedRowNumber(account, index + 1),
CreditRegistryId = account.CreditRegistryId,
DateLastUpdated = DateLastUpdated(account.CreditRegistryId, account.AccountNumber),
AccountNumber = FormattedAccountNumber(account.AccountType, account.AccountNumber)
})
.OrderBy(c=>c.FormattedRecordNumber)
.ThenByDescending(c => c.StateChangeDate);
public DateTime DateLastUpdated(long creditorRegistryId, string accountNo)
{
return (from h in context.AccountHistory
where h.CreditorRegistryId == creditorRegistryId && h.AccountNo == accountNo
select h.LastUpdated).Max();
}
Run Code Online (Sandbox Code Playgroud)
错误是: …
这看起来像一个非常常见的任务,但我找不到一个简单的方法来做到这一点.
我想撤消上次应用的迁移.我本来期待一个简单的命令,比如
PM> Update-Database -TargetMigration:"-1"
Run Code Online (Sandbox Code Playgroud)
相反,我能想出的就是:
PM> Get-Migrations
Retrieving migrations that have been applied to the target database.
201208012131302_Add-SystemCategory
201207311827468_CategoryIdIsLong
201207232247409_AutomaticMigration
201207211340509_AutomaticMigration
201207200025294_InitialCreate
PM> Update-Database -TargetMigration:"CategoryIdIsLong"
Run Code Online (Sandbox Code Playgroud)
(至少我可以只使用名称,跳过时间戳......)
有没有更简单的方法?
entity-framework database-migration entity-framework-4 ef-migrations
当使用需要超过30秒才能完成的函数导入时,我将使用实体框架(EF)获得超时.我尝试了以下操作,但无法解决此问题:
我添加Default Command Timeout=300000到项目中App.Config文件中的连接字符串,该文件具有此处建议的EDMX文件.
这是我的连接字符串的样子:
<add
name="MyEntityConnectionString"
connectionString="metadata=res://*/MyEntities.csdl|res://*/MyEntities.ssdl|
res://*/MyEntities.msl;
provider=System.Data.SqlClient;provider connection string="
Data Source=trekdevbox;Initial Catalog=StarTrekDatabase;
Persist Security Info=True;User ID=JamesTKirk;Password=IsFriendsWithSpock;
MultipleActiveResultSets=True;Default Command Timeout=300000;""
providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)
我尝试直接在我的存储库中设置CommandTimeout,如下所示:
private TrekEntities context = new TrekEntities();
public IEnumerable<TrekMatches> GetKirksFriends()
{
this.context.CommandTimeout = 180;
return this.context.GetKirksFriends();
}
Run Code Online (Sandbox Code Playgroud)
我还能做些什么来让EF超时?这仅适用于非常大的数据集.小数据集一切正常.
这是我得到的错误之一:
System.Data.EntityCommandExecutionException:执行命令定义时发生错误.有关详细信息,请参阅内部异常 ---> System.Data.SqlClient.SqlException:超时已过期.操作完成之前经过的超时时间或服务器没有响应.
好的 - 我得到了这个工作,发生了什么事很愚蠢.我有连接字符串Default Command Timeout=300000和CommandTimeout设置为180.当我Default Command Timeout从连接字符串中删除它,它工作.所以答案是在上下文对象的存储库中手动设置CommandTimeout,如下所示:
this.context.CommandTimeout = 180;
Run Code Online (Sandbox Code Playgroud)
显然在连接字符串中设置超时设置对它没有影响.
c# asp.net entity-framework connection-string entity-framework-4
我已经和它搏斗了一段时间,并且无法弄清楚发生了什么.我有一个卡片实体,其中包含Sides(通常为2个) - 卡片和侧面都有一个舞台.我正在使用EF Codefirst迁移,并且迁移失败并出现此错误:
在表'Sides'上引入FOREIGN KEY约束'FK_dbo.Sides_dbo.Cards_CardId'可能会导致循环或多个级联路径.指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束.
这是我的卡片实体:
public class Card
{
public Card()
{
Sides = new Collection<Side>();
Stage = Stage.ONE;
}
[Key]
[Required]
public virtual int CardId { get; set; }
[Required]
public virtual Stage Stage { get; set; }
[Required]
[ForeignKey("CardId")]
public virtual ICollection<Side> Sides { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的Side实体:
public class Side
{
public Side()
{
Stage = Stage.ONE;
}
[Key]
[Required]
public virtual int …Run Code Online (Sandbox Code Playgroud) 错误消息:
"自创建数据库以来,支持'AddressBook'上下文的模型已经改变.手动删除/更新数据库,或者使用IDatabaseInitializer实例调用Database.SetInitializer.例如,RecreateDatabaseIfModelChanges策略将自动删除并重新创建数据库,并且可选择用新数据播种."
我正在尝试使用代码优先功能,以下是我写的:
var modelBuilder = new ModelBuilder();
var model = modelBuilder.CreateModel();
using (AddressBook context = new AddressBook(model))
{
var contact = new Contact
{
ContactID = 10000,
FirstName = "Brian",
LastName = "Lara",
ModifiedDate = DateTime.Now,
AddDate = DateTime.Now,
Title = "Mr."
};
context.contacts.Add(contact);
int result = context.SaveChanges();
Console.WriteLine("Result :- "+ result.ToString());
}
Run Code Online (Sandbox Code Playgroud)
上下文类:
public class AddressBook : DbContext
{
public AddressBook()
{ }
public AddressBook(DbModel AddressBook)
: base(AddressBook)
{
}
public DbSet<Contact> contacts { get; set; }
public …Run Code Online (Sandbox Code Playgroud) virtual在EF Code First中的属性上使用时,关键字是否有效?有人能描述不同情况下的所有后果吗?
例如,我知道它可以控制延迟加载 - 如果你在ICollection/one-to-many关系属性上使用virtual关键字,默认情况下它将是延迟加载的,而如果你将虚拟关键字保留为out,它将会急切的.
virtual关键字在EF中与POCO实体有什么其他影响?我应该默认使用virtual我的所有属性,还是默认不使用它?
我已经编程了一段时间并且之前使用过LINQ-To-SQL和LINQ-To-Entities(尽管在使用实体时它已经处于实体/表1-1关系 - 即与L2SQL没有太大区别)
我一直在阅读有关控制反转,工作单元,POCO和存储库模式的大量阅读,并希望在我的新应用程序中使用此方法.
我正在努力的是为EF4寻找一个清晰,简明的初学者指南,该指南不承担EF1的知识.
我需要回答的具体问题是:
代码优先/型号第一?关于EF4的优点/缺点(即如果我先编码,在以后更改代码并需要重新生成我的数据库模型会发生什么 - 数据是否会被保留,转换或丢弃?)
假设我要进行代码优先(我想看看EF4如何将其转换为数据库模式)我该如何实际开始?我经常看到有实体图表的文章说"所以这是我的实体模型,现在我要去..." - 不幸的是,我不清楚他们是否在设计师中创建了模型,将其保存到生成代码然后停止任何进一步的自动代码生成 - 或 - 他们编码(POCO)?类和不知何故将它们导入到deisgner视图中?
我想我真正需要的是理解"魔法"来自何处,以及如果我不是直接从DB生成EF模型,如何自己添加它.
我知道这个问题有点模糊,但我不知道我不知道 - 所以任何输入/更正/澄清都会受到赞赏.
毋庸置疑,我不希望有人坐在这里教我EF - 我只是喜欢一些好的教程/论坛/博客/等.对于完整的实体新手
这是桌子
用户
UserId
UserName
Password
EmailAddress
Run Code Online (Sandbox Code Playgroud)
和代码..
public void ChangePassword(int userId, string password){
//code to update the password..
}
Run Code Online (Sandbox Code Playgroud) I have a one entity framework object and when I add it to my project, the connectionstring
is added to app.config in the connectionstring section, but when I want to create new entitycontext and use this connectionstring, this error appears