1.加入Project对象模型
public class Project{[Key][MaxLength(32)]public string ID { get; set; }public string Name { get; set; }public int Order { get; set; }public ICollection<UserStory> Stories { get; set; }public ApplicationUser Creator { get; set; }public DateTime CreateTime { get; set; }public ApplicationUser Modifier { get; set; }public DateTime UpdateTime { get; set; }}
2.将Project关联到Story
public ICollection<UserStory> Stories { get; set; }
3.添加projectcontroller
遇到个bug:
warning CS0169: The field 'UserStoryController.mystr' is never used System.Data.SqlClient.SqlException: Column 'Project.ID' is not the same length or scale as referencing column 'UserStories.ProjectID' in foreign key 'FK_UserStories_Project_ProjectID'. Columns participating in a foreign key relationship must be defined wit h the same length and scale.
Could not create constraint or index. See previous errors.at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
这是由于系统生成的代码不能识别外键字段长度导致。
fix it
migrationBuilder.AddColumn<string>(name: "ProjectID",table: "UserStories",maxLength:32,nullable: true);
启动。一切正常
4. 下面来修改 Create New
删除 创建时间和更新时间,改由服务器赋值。
public async Task<IActionResult> Create(UserStoryViewModel userStoryViewModel){if (ModelState.IsValid){//mapping UserStory usNew = map.Map<UserStory>(userStoryViewModel);usNew.ID = Guid.NewGuid().ToString("N");usNew.StatusCode = "Unstarted";usNew.Creator = _userManager.FindByNameAsync(User.Identity.Name).Result;usNew.CreateTime = DateTime.Now;usNew.Order = _context.UserStories.Max(t => t.Order)+1;_context.UserStories.Add(usNew);await _context.SaveChangesAsync();return RedirectToAction("Index");}return View("OK");}
details 和index删除 createtime
5.menu加入project
6.加入ViewStrories Action
<a asp-action="ViewStories" asp-route-id="@item.ID">View Stories</a>
7.修改StoryController加入解析id
完成。