本文介绍了同时节省不为他们的关系公开外键属性的实体时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在实体框架4.1 代码首先一个简单的代码: PasmISOContext DB =新PasmISOContext(); VAR用户=新用户(); user.CreationDate = DateTime.Now; user.LastActivityDate = DateTime.Now; user.LastLoginDate = DateTime.Now; db.Users.Add(用户); db.SaveChanges(); user.Avatar =新的头像(){链接=新的URI(HTTP:// myUrl /%2E%2E /%2E%2E)}; db.SaveChanges(); db.Users.Add(新用户(){阿凡达=新的头像(){链接=新的URI(HTTP:// myUrl /%2E%2E /%2E% 2E)}}); db.SaveChanges(); 的问题是,我得到一个错误 同时节省不为他们的关系暴露的外键性质的实体时发生错误。该EntityEntries属性将返回null,因为一个单独的实体不能被认定为异常的源。异常处理,同时节省可以做成。通过在你的实体类型揭露外键的属性更容易。见设置InnerException了解详细信息。 在 db.Users.Add(新用户(){阿凡达=新的头像(){链接=新的URI(HTTP:// myUrl /%2E%2E /%2E%2E)} }); db.SaveChanges(); 我不明白,为什么同样的操作工作。是不是有什么毛病我的模型,或EF-代码优先? 公共类阿凡达 { [关键词] 公众诠释标识{搞定;组; } [必填] 公共字符串LinkInString {搞定;组; } [NotMapped] 公众开放的链接 { {返回新的URI(LinkInString); } 集合{LinkInString = value.AbsoluteUri; } } } 公共类用户 { [关键] 公众诠释标识{搞定;组; } 公共字符串用户名{获得;组; } 公共字符串电子邮件{获得;组; } 公共字符串密码{搞定;组; } 公共头像头像{搞定;组; } 公共虚拟的ICollection<问题>问题{搞定;组; } 公共虚拟的ICollection<&成就GT;成就{搞定;组; } 公众的DateTime CreationDate {搞定;组; } 公众的DateTime LastLoginDate {搞定;组; } 公众的DateTime LastActivityDate {搞定;组; } } 解决方案 对于那些你们谁仍然有这样的错误与正确定义所有按键,看看你的实体,并确保你不会留下一个日期时间字段为空值。 I have a simply code in Entity Framework 4.1 code first:PasmISOContext db = new PasmISOContext();var user = new User();user.CreationDate = DateTime.Now;user.LastActivityDate = DateTime.Now;user.LastLoginDate = DateTime.Now;db.Users.Add(user);db.SaveChanges();user.Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") };db.SaveChanges();db.Users.Add(new User() { Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") } });db.SaveChanges();The problem is that I get an error An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.at db.Users.Add(new User() { Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") } });db.SaveChanges();I don't understand why the similar operation works. Is there something wrong with my model, or with ef-code-first?public class Avatar{ [Key] public int Id { get; set; } [Required] public string LinkInString { get; set; } [NotMapped] public Uri Link { get { return new Uri(LinkInString); } set { LinkInString = value.AbsoluteUri; } }}public class User{ [Key] public int Id { get; set; } public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public Avatar Avatar { get; set; } public virtual ICollection<Question> Questions { get; set; } public virtual ICollection<Achievement> Achievements { get; set; } public DateTime CreationDate { get; set; } public DateTime LastLoginDate { get; set; } public DateTime LastActivityDate { get; set; }} 解决方案 For those of you who would still have this error with all keys properly defined, have a look at your entities and make sure you don't leave a datetime field with a null value. 这篇关于同时节省不为他们的关系公开外键属性的实体时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 10:56