这看起来很简单,但我无法让它发挥作用。我有两个表,Organization
和Season
。
Organization
表可以设置活动季节,必须使用Organization
设置创建季节。
Organization
class(table)
public class Organization
{
public int ID { get; set; }
public string Name { get; set; }
[Display(Name = "Active Season")]
public int? ActiveSeasonID { get; set; }
public virtual Season ActiveSeason { get; set; }
}
Season
class(table)
public class Season
{
public int ID { get; set; }
public int OrganizationID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime EndDate { get; set; }
public string Name { get; set; }
public virtual Organization organization { get; set; }
}
给我这个错误:
无法确定类型“ResultzSys.DAL.Organization”和“ResultzSys.DAL.Season”之间关联的主要结束。必须使用关系流畅API或数据注释显式配置此关联的主要结尾。
所以我有这个来定义两个
之间的关系modelBuilder.Entity<Organization>()
.HasOptional(s => s.ActiveSeason).WithRequired(d => d.organization);
但是当我尝试添加季节时我会得到
当IDENTITY_INSERT设置为OFF时,无法在表'Season'中为identity列插入显式值
抛出该错误的控制器代码:
public async Task<ActionResult> Create([Bind(Include = "ID,OrganizationID,StartDate,EndDate,Name")] Season season)
{
if (ModelState.IsValid)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ResultzContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
season.OrganizationID = currentUser.OrganizationID;
db.Seasons.Add(season);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
}
我根本不明白为什么它不会让我创造赛季纪录......任何提示都会受到赞赏。
谢谢
答案 0 :(得分:0)
根据this MSDN article,您应在身份列上使用DatabaseGenerated
属性:
[DatabaseGenerated(DatabaseGenerationOption.Identity)]
public int ID { get; set; }