【发布时间】:2017-12-10 02:10:31
【问题描述】:
我有两个模型: 用户资料
public class UserProfile
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<UserPost> UserPost { get; set; }
}
用户帖子:
public class UserPost
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
public string PostTitle { get; set; }
public virtual UserProfile UserProfile { get; set; }
public string Contents { get; set; }
}
UserProfile 与 AspNetUser 是一对一的关系。
UserProfile 与 UserPost 是一对多的关系。
当我在控制器的 Create 方法中将 UserPost 添加到数据库时,我得到一个 System.Data.Entity.Infrastructure.DbUpdateException
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Create([Bind(Include = "PostTitle, Content")] UserPost post)
{
if (ModelState.IsValid)
{
UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var user = UserManager.FindById(User.Identity.GetUserId());
post.UserProfile = user.UserProfile;
db.UserPosts.Add(post);
db.SaveChanges();//error here
return RedirectToAction("Index");
}
return View(post);
}
发生 System.Data.Entity.Infrastructure.DbUpdateException H结果=0x80131501 Message=保存不为其关系公开外键属性的实体时发生错误。 EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地处理保存时的异常。有关详细信息,请参阅 InnerException。
内部异常1:UpdateException:更新时出错 条目。有关详细信息,请参阅内部异常。
内部异常 2:SqlException:无法将值 NULL 插入 列“Id”,表“aspnet-project12017.dbo.UserPosts”;列确实 不允许空值。插入失败。声明已终止。
【问题讨论】:
-
要么将 UserPost
Id列更改为int:public int Id { get; set; },要么将其设置为[DatabaseGenerated(DatabaseGeneratedOption.None)]。 AFAIK 您不能将字符串/GUID 列设置为自动生成的值列,它仅适用于int、long或其他数字数据类型(请参阅stackoverflow.com/questions/8855100/…)。 -
@TetsuyaYamamoto 因此,如果我想将我的 ID 保留为字符串,那么在创建新帖子时如何生成它,因为它不是自动生成的?
-
您可以从类构造函数中手动生成它,例如
public UserPost() { Id = Guid.NewGuid().ToString(); }。当然,您可以在那里设置另一个值而不是 GUID。 -
我尝试将 Id 更改为 int,但仍然出现相同的错误。 @TetsuyaYamamoto
-
这是代码优先,对吧?如果您已经在创建包含数据的数据库并且仍然遇到相同的错误,请使用 EF Code First Migration 更改标识列数据类型。只要目标表中
Id的数据类型不变,EF 仍将该列视为string而不是int。
标签: c# mysql sql-server asp.net-mvc entity-framework